dcast a data.table in r
dcast a data.table in r
a1 <- data.table(id=c(1,1,1,1,2,2,2,3,3),
var=c("6402","1","6302","3","6406","6406","2","1","1"))
b1 <- data.table(var=c("6402","6406","6302"),
txt=c("A","B","A"))
mm <- b1[a1,on=.(var)]
dcast(mm,id~txt,function(x) any(!is.na(x)),fill=NA)
desired_output <- data.table(id=c(1,2,3),
A=c(T,F,F),
B=c(F,T,F))
如何获得 desired_output?不知怎么的,聚合功能好像在跟我玩游戏...
创建 id
因子变量并在删除 NA
行后将 dcast
与 drop = FALSE
一起使用。
library(data.table)
mm$id <- factor(mm$id)
dcast(na.omit(mm),id~txt,function(x) any(!is.na(x)), drop = FALSE)
# id A B
#1: 1 TRUE FALSE
#2: 2 FALSE TRUE
#3: 3 FALSE FALSE
a1 <- data.table(id=c(1,1,1,1,2,2,2,3,3),
var=c("6402","1","6302","3","6406","6406","2","1","1"))
b1 <- data.table(var=c("6402","6406","6302"),
txt=c("A","B","A"))
mm <- b1[a1,on=.(var)]
dcast(mm,id~txt,function(x) any(!is.na(x)),fill=NA)
desired_output <- data.table(id=c(1,2,3),
A=c(T,F,F),
B=c(F,T,F))
如何获得 desired_output?不知怎么的,聚合功能好像在跟我玩游戏...
创建 id
因子变量并在删除 NA
行后将 dcast
与 drop = FALSE
一起使用。
library(data.table)
mm$id <- factor(mm$id)
dcast(na.omit(mm),id~txt,function(x) any(!is.na(x)), drop = FALSE)
# id A B
#1: 1 TRUE FALSE
#2: 2 FALSE TRUE
#3: 3 FALSE FALSE