怎么样:
if(length(unique(cl <- sapply(dat, class))) > 1 &&
any(!sapply(dat, is.numeric))) {
dat <- dat[ , which(cl == cl[1]), drop = FALSE]
}
这假设在以下示例中:
dat2 <- data.frame(A = factor(sample(LETTERS, 26, replace = TRUE)),
B = factor(sample(LETTERS, 26, replace = TRUE)),
C = sample(LETTERS, 26, replace = TRUE),
dat, stringsAsFactors = FALSE)
> sapply(dat2, class)
A B C
"factor" "factor" "character"
as.integer.1.26. as.integer.26.1. as.numeric.1.26.
"integer" "integer" "numeric"
您只需要因子变量,即您想要区分字符变量和因子变量 - 这就是您的代码似乎要做的事情。
对于这个例子,我使用
if(length(unique(cl <- sapply(dat2, class))) > 1 &&
any(!sapply(dat2, is.numeric))) {
dat2 <- dat2[ ,which(cl == cl[1]), drop = FALSE]
}
这导致
> head(dat2)
A B
1 D G
2 P D
3 C T
4 X F
5 N R
6 A E
> sapply(dat2, class)
A B
"factor" "factor"
上dat
,上面的if()
语句不会改变dat
:
> if(length(unique(cl <- sapply(dat, class))) > 1 &&
+ any(!sapply(dat, is.numeric))) {
+ dat <- dat[ , which(cl == cl[1]), drop = FALSE]
+ }
> head(dat)
as.integer.1.26. as.integer.26.1. as.numeric.1.26.
1 1 26 1
2 2 25 2
3 3 24 3
4 4 23 4
5 5 22 5
6 6 21 6