在data.table中创建子组

时间:2019-06-28 09:56:10

标签: r data.table melt

说我有以下简化的数据集:

<=4

我想替换let letter = prefixes[bingoBall/18] 中所有值为2的值。替换值在dt2中给出。可以通过id将两个表连接起来。 如果最终值不等于2,则最终值应保持不变。如果等于2,则最终值应变为dt <- data.table(id = 1:5, val = c(1, 2, 3, 2, 4)) dt2 <- data.table(id = c(2, 4), val = c(2, 3))

所需的输出:

dt

我尝试过的方法(它可以工作,但看起来并不优雅):

paste0(dt$val, ".", dt2$val)

问题:如何更好地进行转换?

2 个答案:

答案 0 :(得分:3)

您正在寻找更新加入

dt[dt2, on=.(id), val := paste0(x.val, ".", i.val)]

输出:

   id val
1:  1   1
2:  2 2.2
3:  3   3
4:  4 2.3
5:  5   4

数据:

#val column needs to be of character type to suppress the warning
dt <- data.table(id = 1:5, val = as.character(c(1, 2, 3, 2, 4)))
dt2 <- data.table(id = c(2, 4), val = c(2, 3)) 

答案 1 :(得分:2)

library(data.table)

# example data
dt <- data.table(id = 1:5, val = c(1, 2, 3, 2, 4))
dt2 <- data.table(id = c(2, 4), val = c(2, 3)) 

如果两个数据集都正确排序,则可以使用基数R,如下所示:

dt$val[dt$id %in% dt2$id] = paste0(dt$val[dt$id %in% dt2$id], ".", dt2$val)

dt

#    id val
# 1:  1   1
# 2:  2 2.2
# 3:  3   3
# 4:  4 2.3
# 5:  5   4

否则,您可以使用此:

dt_merged = merge(dt, dt2, by="id", all.x=T)[, val:=ifelse(is.na(val.y), 
                                                           val.x, 
                                                           paste0(val.x, ".", val.y))]
dt_merged = dt_merged[, c("id","val")]
dt_merged

#    id val
# 1:  1   1
# 2:  2 2.2
# 3:  3   3
# 4:  4 2.3
# 5:  5   4