我有一个data.table,如:
Cell Num
a 1,2,3,4,5,6
b 7,8,9
c 10,11,12
d 13,14
我需要将其转换为:
Num Cell
1 a
2 a
3 a
4 a
5 a
6 a
7 b
8 b
9 b
10 c
11 c
12 c
13 d
14 d
我们怎样才能将表格分成所需的格式?
答案 0 :(得分:2)
我们可以使用cSplit
library(splitstackshape)
cSplit(df1, "Num", ",", "long")
# Cell Num
# 1: a 1
# 2: a 2
# 3: a 3
# 4: a 4
# 5: a 5
# 6: a 6
# 7: b 7
# 8: b 8
# 9: b 9
#10: c 10
#11: c 11
#12: c 12
#13: d 13
#14: d 14
或者@David Arenburg提到
library(data.table)
dt[, .(Num = strsplit(Num, ",")[[1]]), by = Cell]