我的一个名为type
的变量看起来像这样:
$ type : Factor w/ 4 levels "","a","e","u":
我想将变量type
的空因子水平与因子水平"u"
合并
我已经使用函数levels
来实现这一目标,但是我对此解决方案并不感到满意。
levels(mydata$type) = list( u = "", a = "a", e = "e", u = "u")
是否有更多类似于“ data.table”的解决方案来合并R中因子变量的级别?
您的方法:
library(data.table)
set.seed(100)
mydata = data.table(x = runif(20),
type=factor(sample(c("","a","e","u"),20,replace=TRUE)))
levels(mydata$type) = list( u = "", a = "a", e = "e", u = "u")
table(mydata$type)
u a e
11 4 5
str(mydata$type)
Factor w/ 3 levels "u","a","e": 3 1 2 1 1 3 3 1 2 1 ...
我不知道使用replace
是否像data.table一样,但是您本质上需要转换回字符,进行替换和重构:
set.seed(100)
mydata = data.table(x = runif(20),
type=factor(sample(c("","a","e","u"),20,replace=TRUE)))
mydata[,type :=droplevels(replace(type,type=="","u"))]
table(mydata$type)
a e u
4 5 11
str(mydata$type)
Factor w/ 3 levels "a","e","u": 2 3 1 3 3 2 2 3 1 3 ...