如何在数据表中“取消列出”列
在我的表格中,某些单元格是向量而不是单个值,即列是列表而不是向量:
in my table, some cells are vectors instead of single value, i.e. the column is a list instead of vector:
dt1 <- data.table(
colA= c('A1','A2','A3'),
colB=list('B1',c('B2a','B2b'),'B3'),
colC= c('C1','C2','C3'),
colD= c('D1','D2','D3')
)
dt1
# colA colB colC colD
#1: A1 B1 C1 D1
#2: A2 B2a,B2b C2 D2
#3: A3 B3 C3 D3
我需要将其重塑为长格式,以取消列出该列 colB
。到目前为止,我是这样的:
I need to reshape it to a long format unlisting that column colB
. So far I do it like this:
dt1[,.(colB=unlist(colB)),by=.(colA,colC,colD)]
# colA colC colD colB
#1: A1 C1 D1 B1
#2: A2 C2 D2 B2a
#3: A2 C2 D2 B2b
#4: A3 C3 D3 B3
它确实可以胜任,但我不喜欢必须在 by =
中明确指出所有其他列名称。有更好的方法吗?
(我确信它已经在其他地方得到解答了,但到目前为止我找不到)
it does the job but I don't like that I have to indicate all other column names explicitly in by=
. Is there better way to do this?
(I'm sure it's already answered elsewhere but I couldn't find it so far)
P.S。理想情况下,我想在没有任何外部软件包的情况下进行管理
P.S. ideally I would like to manage without any external packages
将我的评论升级为答案。使用:
Promoting my comment to an answer. Using:
dt1[,.(colB = unlist(colB)), by = setdiff(names(dt1), 'colB')]
给出:
colA colC colD colB
1: A1 C1 D1 B1
2: A2 C2 D2 B2a
3: A2 C2 D2 B2b
4: A3 C3 D3 B3
(@Frank的建议略有不同):
Or as an alternative (a slight variation of @Frank's proposal):
dt1[rep(dt1[,.I], lengths(colB))][, colB := unlist(dt1$colB)][]