如何使用合并的列名合并data.table?

3
我正在使用dcast.data.table将一个长数据表转换为宽数据表。
library(data.table)
library(reshape2)

set.seed(1234)
dt.base <- data.table(A = rep(c(1:3),2), B = rep(c(1:2),3), C=c(1:4,1,2),thevalue=rnorm(6))


#from long to wide using dcast.data.table()
dt.cast <- dcast.data.table(dt.base, A ~ B + C, value.var = "thevalue", fun = sum)

#now some stuff happens e.g., please do not bother what happens between dcast and melt
setkey(dt.cast, A)
dt.cast[2, c(2,3,4):=1,with = FALSE]

现在我想将data.table再次还原为原始列布局,但是我卡住了,我该如何从转换后的data.table中分离出拼接的列名呢?这就是我的问题。

dt.melt <- melt(dt.cast,id.vars = c("A"), value.name = "thevalue")

我需要两列而不是一列

使用以下代码可以得到我想要的结果

#update
dt.base[A==2 & B == 1 & C == 1, thevalue :=1]
dt.base[A==2 & B == 2 & C == 2, thevalue :=1]

#insert (2,1,3 was not there in the base data.table)
dt.newrow <- data.table(A=2, B=1, C=3, thevalue = 1)
dt.base <-rbindlist(list(dt.base, dt.newrow))
dt.base

一如既往,感谢您的帮助。

(注:本段内容无需翻译)


3
请展示所需的输出,目前不清楚您希望最终结果是什么样子。可能您只需拼接dt.meltvariable列即可。 - Ricardo Saporta
@RicardoSaporta 我添加了一些额外的代码,以产生我想要的结果。但与此同时,David提供了一个很好的解决方案,现在我只是想知道是否可以直接从melt函数中实现这个功能。 - Tom Martens
1个回答

3
你觉得这样行吗?
colnames <- c("B", "C")
dt.melt[, (colnames) := (colsplit(variable, "_", colnames))][, variable := NULL]
subset(dt.melt, thevalue != 0)
# or dt.melt[thevalue != 0, ]

#   A   thevalue B C
#1: 1 -1.2070657 1 1
#2: 2  1.0000000 1 1
#3: 2  1.0000000 1 3
#4: 3  1.0844412 1 3
#5: 2  1.0000000 2 2
#6: 3  0.5060559 2 2
#7: 1 -2.3456977 2 4

如果您的数据集不可表示,并且有效行中可能有零,则可以采用另一种方法。
colnames <- c("B", "C")
setkey(dt.melt[, (colnames) := (colsplit(variable, "_",colnames))][, variable := NULL], A, B, C)
setkey(dt.base, A, B, C)
dt.base <- dt.melt[rbind(dt.base, data.table(A = 2, B = 1, C = 3), fill = T)]
dt.base[, thevalue.1 := NULL]

##    A B C   thevalue
## 1: 1 1 1 -1.2070657
## 2: 1 2 4 -2.3456977
## 3: 2 1 1  1.0000000
## 4: 2 2 2  1.0000000
## 5: 3 1 3  1.0844412
## 6: 3 2 2  0.5060559
## 7: 2 1 3  1.0000000

编辑

正如 @Arun 建议的那样,最有效的方法是使用 @AnandaMahto 的 cSplit 函数,因为它也使用了 data.table,即:

cSplit(dt.melt, "variable", "_")

第二次编辑

为了避免手动合并,您可以在使用 dcast 进行转换时设置 fill = NA(例如),然后使用 csplit 一次性完成所有操作,例如:

dt.cast <- dcast.data.table(dt.base, A ~ B + C, value.var = "thevalue", fun = sum, fill = NA)
setkey(dt.cast, A)
dt.cast[2, c(2,3,4):=1,with = FALSE]
dt.melt <- melt(dt.cast,id.vars = c("A"), value.name = "thevalue")
dt.cast <- cSplit(dt.melt, "variable", "_")[!is.na(thevalue)]
setnames(dt.cast, 3:4, c("B","C"))

#    A   thevalue B C
# 1: 1 -1.2070657 1 1
# 2: 2  1.0000000 1 1
# 3: 2  1.0000000 1 3
# 4: 3  1.0844412 1 3
# 5: 2  1.0000000 2 2
# 6: 3  0.5060559 2 2
# 7: 1 -2.3456977 2 4

比那复杂一点,以避免删除原有的零值 :-) 这更多是与“colsplit”有关 :-) - Tom Martens
1
遗憾的是,colsplit 不会很好地扩展。请尝试改用 unlist(strsplit(x, "_")) - Ricardo Saporta
1
尝试使用@AnandaMahto的cSplit函数。您可以在此处获取它:https://gist.github.com/mrdwab/11380733。 - Arun
2
@Arun,你说得对。不知道为什么我没想到它已经在我的机器上了... - David Arenburg
1
@TomMartens,请看我的最后一次编辑,关于设置fill = NA并使用cSplit。我认为这是最好的方法。 - David Arenburg
显示剩余6条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接