【发布时间】:2020-04-18 04:34:27
【问题描述】:
我有以下数据框
df <- data.frame(x=c("one", "one, two", "two, three", "one, two, three"))
看起来像这样
x
1 one
2 one, two
3 two, three
4 one, two, three
我希望能够将此x 列分成许多不同的列,一个对应于x 列中的每个不同 字。基本上我希望最终结果是这样的
one two three
1 1 0 0
2 1 1 0
3 0 1 1
4 1 1 1
我认为为了获得该数据帧,我可能需要能够使用tidyr 提供的separate 函数并记录here。但是,这需要了解正则表达式,而我对它们并不擅长。谁能帮我获取这个数据框?
重要提示:我不知道数字,也不知道单词的拼写。
重要示例
它也应该适用于空字符串。例如,如果我们有
df <- data.frame(x=c("one", "one, two", "two, three", "one, two, three", ""))
那么它也应该可以工作。
【问题讨论】:
-
试试
library(splitstackshape); cSplit_e(df, split.col = "x", fixed = TRUE, type = "character", drop = TRUE, fill = 0L) -
@markus 我会看看那个问题
标签: r regex tidyverse tidyr regex-lookarounds