R / data.table()合并另一个data.table的命名子集 [英] R / data.table() merge on named subset of another data.table

查看:1039
本文介绍了R / data.table()合并另一个data.table的命名子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想把几个文件放在一起,需要对在循环中创建的列名称进行一系列的合并。我可以使用 data.frame()但有问题使用类似的代码与 data.table()

I'm trying to put together several files and need to do a bunch of merges on column names that are created inside a loop. I can do this fine using data.frame() but am having issues using similar code with a data.table():

library(data.table)

df1 <- data.frame(id = 1:20, col1 =  runif(20))
df2 <- data.frame(id = 1:20, col1 =  runif(20))

newColNum <- 5
newColName <- paste('col',newColNum ,sep='')

df1[,newColName] <- runif(20)

df2 <- merge(df2, df1[,c('id',newColName)], by = 'id', all.x = T) # Works fine
######################

dt1 <- data.table(id = 1:20, col1 =  runif(20))
dt2 <- data.table(id = 1:20, col1 =  runif(20))

newColNum <- 5
newColName <- paste('col',newColNum ,sep='')

dt1[,newColName] <- runif(20)

dt2 <- merge(dt2, dt1[,c('id',newColName)], by = 'id', all.x = T) # Doesn't work


推荐答案

这真的与 merge()无关,如何 j (ie列)的索引默认为 [。data.table()

This really has nothing to do with merge(), and everything to do with how the j (i.e. column) index is, by default, interpreted by [.data.table().

通过将设置为= FALSE ,这将导致 j 索引被解释为通过 data.frame

You can make the whole statement work by setting with=FALSE, which causes the j index to be interpreted as it would be in a data.frame:

dt2 <- merge(dt2, dt1[,c('id',newColName), with=FALSE], by = 'id', all.x = T)
head(dt2, 3)
#    id      col1       col5
# 1:  1 0.4954940 0.07779748
# 2:  2 0.1498613 0.12707070
# 3:  3 0.8969374 0.66894157


$ b b

更精确地,从?data.table


<默认情况下'with = TRUE'和'j'在'x'的框架
中计算。列名称可用作变量。
'with = FALSE'时,'j'是向
选择的名称或位置的向量。

这篇关于R / data.table()合并另一个data.table的命名子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆