我有一个包含字符串规则集的数据集。
R> input
id rules
1 1 1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0
2 2 1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0
3 3 1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0
4 4 1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0
5 5 1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0
6 6 1.11=>0;1.12=>0;1.13=>0;1.14=>1;1.15=>0
7 7 1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0
8 8 1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0
9 9 1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0
10 10 1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0
11 11 1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0
12 12 1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0
13 13 1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0
14 14 1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0
15 15 1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0
16 16 1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0
将此规则拆分和合并为单独的列的最快方法是什么?期望的结果:
R> res
R1.11 R1.12 R1.13 R1.14 R1.15 id
1 0 0 0 0 0 1
2 0 0 0 0 0 2
3 0 0 0 0 0 3
4 0 0 0 0 0 4
5 0 0 0 0 0 5
6 0 0 0 1 0 6
7 0 0 0 0 0 7
8 0 0 0 0 0 8
9 0 0 0 0 0 9
10 0 0 0 0 0 10
11 0 0 0 0 0 11
12 0 0 0 0 0 12
13 0 0 0 0 0 13
14 0 0 0 0 0 14
15 0 0 0 0 0 15
16 0 0 0 0 0 16
要重现数据集,请参阅下面的结构。
输入数据结构:
input <- structure(
list(id = 1:16,
rules = c("1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0",
"1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0",
"1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0",
"1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0",
"1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0",
"1.11=>0;1.12=>0;1.13=>0;1.14=>1;1.15=>0",
"1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0",
"1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0",
"1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0",
"1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0",
"1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0",
"1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0",
"1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0",
"1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0",
"1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0",
"1.11=>0;1.12=>0;1.13=>0;1.14=>0;1.15=>0")),
.Names = c("id", "rules"),
row.names = c(NA, -16L),
class = "data.frame")
输出数据结构:
output <- structure(
list(R1.11 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
R1.12 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
R1.13 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
R1.14 = c(0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
R1.15 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
id = 1:16),
.Names = c("R1.11", "R1.12", "R1.13", "R1.14", "R1.15", "id"),
class = "data.frame",
row.names = c(NA, -16L))
最佳答案
我可能会...
library(splitstackshape)
res = cSplit(input, "rules", ";", "long")
res[, c("variable", "value") := tstrsplit(rules, "=>", type.convert=TRUE)]
# head(res)
# id rules variable value
# 1: 1 1.11=>0 1.11 0
# 2: 1 1.12=>0 1.12 0
# 3: 1 1.13=>0 1.13 0
# 4: 1 1.14=>0 1.14 0
# 5: 1 1.15=>0 1.15 0
# 6: 2 1.11=>0 1.11 0
我会在这里停下来,使用长格式的数据,但您可以使用...转到您想要的宽输出
wideres = dcast(res, id ~ paste0("R", variable), value.var="value")
# test that it's essentially correct:
fsetequal(wideres, setcolorder(data.table(output), names(wideres)))
关于r - 将规则字符串解析为 data.frame 的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41938602/