我有一个名称不同的专栏:
X <- c("Ashley, Tremond WILLIAMS, Carla", "Claire, Daron", "Luw, Douglas CANSLER, Stephan")
在第二个空格之后,它会启动第二个人的名字。例如,阿什利,特雷蒙德是一个人,而威廉姆斯,卡拉是另一个人。
我试过了:
strsplit(X, "\\,\\s|\\,|\\s")
但它除以所有空格,所以我得到:
strsplit(X, "\\,\\s|\\,|\\s")
[[1]]
[1] "Ashley" "Tremond" "WILLIAMS" "Carla"
[[2]]
[1] "Claire" "Daron"
[[3]]
[1] "Luw" "Douglas" "CANSLER" "Stephan"
我怎样才能在第一个空格后分开,所以我得到了?:
[1] "Ashley, Tremond" "WILLIAMS, Carla"
[[2]]
[1] "Claire, Daron"
[[3]]
[1] "Luw, Douglas" "CANSLER, Stephan"
提前感谢您的所有帮助
答案 0 :(得分:-1)
当然@ ytk的评论有效,但如果你想避免使用正则表达式, 你可以偷偷摸摸地做,
df2 <- df %>%
separate(col = X, into=c("person1a","person1b","person2a","person2b"),sep= " ") %>%
unite(col = "person1", person1a, person1b, sep=" ") %>%
unite(col = "person2", person2a, person2b, sep=" ")
返回:
> df2
person1 person2
1 Ashley, Tremond WILLIAMS, Carla
2 Claire, Daron NA NA
3 Luw, Douglas CANSLER, Stephan
P.S。我使用df <- data.frame(X = c("Ashley, Tremond WILLIAMS, Carla", "Claire, Daron", "Luw, Douglas CANSLER, Stephan"))
将输入转换为数据帧。