R语言的因子Factor

Factor数量不因值的减少而减少
x <- as.factor(sample(head(colors()),100,replace=TRUE))
levels(x)
x <- x[x!=”aliceblue”]
levels(x) # still the same levels
table(x) # even though one level has 0 entries!
如果需要改为当前的Factor
x <- factor(x) #set factor once more
levels(x) #lost 1 level 使用levels(droplevels(x))也可以查看当前的Factor
对于list: subdf[] <- lapply(subdf, function(x) if(is.factor(x)) factor(x) else x)


默认的,data frame里的character strings会自动转换为factors
(只有data frame会)
df <- data.frame(letters=c('a','b','c','d','e'), numbers=seq(1:5)) \>str(df)
‘data.frame': 5 obs. of 2 variables:
$ letters: Factor w/ 5 levels “a”,”b”,”c”,”d”,..: 1 2 3 4 5
$ numbers: int 1 2 3 4 5
stringsAsFactors = FALSE可以防止这种自动转换
把data.frame里的Factor改为character类型
方法1:bob[] <- lapply(bob, as.character)
方法2:bob <- data.frame(lapply(bob, as.character), stringsAsFactors=FALSE)

发表评论