簡體   English   中英

R中的合並因子水平data.table方式

[英]merging factor levels data.table way in R

我的一個名為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 ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM