[英]R: Merge data.table and fill in NAs
Suppose 3 data tables: 假设有3个数据表:
dt1<-data.table(Type=c("a","b"),x=1:2)
dt2<-data.table(Type=c("a","b"),y=3:4)
dt3<-data.table(Type=c("c","d"),z=3:4)
I want to merge them into 1 data table, so I do this: 我想将它们合并到1个数据表中,所以我这样做:
dt4<-merge(dt1,dt2,by="Type") # No error, produces what I want
dt5<-merge(dt4,dt3,by="Type") # Produces empty data.table (0 rows) of 4 cols: Type,x,y,z
Is there a way to make dt5 like this instead?: 有没有办法让dt5代替这样?:
> dt5
Type x y z
1: a 1 3 NA
2: b 2 4 NA
3: c NA NA 3
4: d NA NA 4
If you know in advance the unique values you have in your Type
column you can use J
and then join tables the data.table
way. 如果您事先知道Type
的唯一值,则可以使用J
,然后以data.table
方式连接表。 You should set the key for each table so data.table
knows what to join on, like this... 您应该为每个表设置密钥,以便data.table
知道要加入的内容,例如...
# setkeys
setkey( dt1 , Type )
setkey( dt2 , Type )
setkey( dt3 , Type )
# Join
dt1[ dt2[ dt3[ J( letters[1:4] ) , ] ] ]
# Type x y z
#1: a 1 3 NA
#2: b 2 4 NA
#3: c NA NA 3
#4: d NA NA 4
This shows off data.table
's compound queries (ie dt1[dt2[dt3[...]]]
) which are wicked! 这显示了data.table
的复合查询(即dt1[dt2[dt3[...]]]
)这些都是邪恶的!
If you don't know in advance the unique values for the key column you can make a list of your tables and use lapply
to quickly run through them getting the unique values to make your J
expression... 如果你事先不知道键列的唯一值,你可以列出你的表,并使用lapply
快速浏览它们获取唯一值来制作你的J
表达式......
# A simple way to get the unique values to make 'J',
# assuming they are in the first column.
ll <- list( dt1 , dt2 , dt3 )
vals <- unique( unlist( lapply( ll , `[` , 1 ) ) )
#[1] "a" "b" "c" "d"
Then use it like before, ie dt1[ dt2[ dt3[ J( vals ) , ] ] ]
. 然后像以前一样使用它,即dt1[ dt2[ dt3[ J( vals ) , ] ] ]
。
While you explore the all
argument to merge
, I'll also offer you an alternative that might want to consider: 当您探索要merge
的all
参数时,我还会为您提供可能需要考虑的替代方案:
Reduce(function(x, y) merge(x, y, by = "Type", all = TRUE), list(dt1, dt2, dt3))
# Type x y z
# 1: a 1 3 NA
# 2: b 2 4 NA
# 3: c NA NA 3
# 4: d NA NA 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.