这是我工作的R语言:
我有一个大型数据集,并尝试拆分该列中具有不同数量值的列进行拆分。分隔符是" |"由于无法锻炼的原因,数据看起来像这样:
No transportation
No transportation | Physically Unable | Don't know how
Physically Unable | No transportation
No Money | Don't know how | No Facilities | No free time
我希望将这些列添加到不同的列中,并为每个输入的值添加新列。我试图使用dplyr中的代码:tidyr:
data <- data %>% separate(Exercise_Reasons, 'Exercise_No', paste("Exercise_No", 1:5, sep="|"), extra="drop")
从splitstackshape尝试过这个:
cSplit(data,Exercise_Reasons,sep="|")
我非常感谢任何帮助,因为我是R
的新手答案 0 :(得分:2)
您的单独调用无法正常工作的原因是|
是一个正则表达式字符,因此您需要将其转义为分隔符。
这有效:
a
V1
1 No transportation
2 No transportation | Physically Unable | Don't know how
3 Physically Unable | No transportation
4 No Money | Don't know how | No Facilities | No free time
tidyr::separate(a, V1, into = letters[1:4], sep = "\\|")
a b c d
1 No transportation <NA> <NA> <NA>
2 No transportation Physically Unable Don't know how <NA>
3 Physically Unable No transportation <NA> <NA>
4 No Money Don't know how No Facilities No free time