【发布时间】:2021-03-11 09:17:37
【问题描述】:
您好,我有这个数据框 (DF1)
structure(list(Value = list("Peter", "John", c("Patric", "Harry")),Text = c("Hello Peter How are you","Is it John? Yes It is John, Harry","Hello Patric, how are you. Well, Harry thank you.")) , class = "data.frame", row.names = c(NA, -3L))
Value Text
1 Peter Hello Peter How are you
2 John Is it John? Yes It is John, Harry
3 c(Patric, Harry) Hello Patric, how are you. Well, Harry thank you.
我想将 Text 中的句子按 Value 中的名称拆分以得到这个
Value Text Split
1 Peter Hello Peter How are you c("Hello", "Peter How are you")
2 John Is it John? Yes It is John, Harry c("Is it", "John? Yes It is John, Harry")
3 c(Patric, Harry) Hello Patric, how are you. Well, Harry thank you c("Hello", "Patric, how are you. Well,", "Harry thank you")
我试过了
DF1 %>% mutate(Split = strsplit(as.character(Text),as.character(Value)))
但它不起作用
【问题讨论】:
-
我认为你的数据有些奇怪。
c(Patric, Harry)似乎是数据准备错误的结果。我希望价值在structure中被识别如下:Value = list("Peter", "John", c("Patric", "Harry")) -
谢谢我刚刚修复它
-
我留下了两种可能的解决方案。看看他们。
-
谢谢,我会测试它们并告诉你。
-
@Edo 谢谢,它运作良好。 In Value 不能是任何字符,只能是字母和数字。