【发布时间】:2021-06-16 04:33:01
【问题描述】:
我有一个非常简单的需求:在 dplyr 管道链中将一列拆分为两个新列。这里的技巧是使用特定的单词作为分隔符而不是单个字符。
数据:
id elements
1 banana and apple
2 orange and lemon
3 house and flat
预期结果
id element1 element2
1 banana apple
2 orange lemon
3 house flat
显然,tidyr::separate 方法没有按预期工作(我的错)。用“and”的首字母分隔。
df %>% tidyr::separate(elements, into = c("element1","element2"), sep = "and")
我知道这也许可以用其他动词来实现,但我的主要目标是尽可能使用 dplyr 和 tidyr。
【问题讨论】:
-
你能
dput你的数据吗? -
@Forge 当你说没有按预期工作时不清楚?我得到正确的输出。只是为了删除空格,我添加了
\\s*。当你使用单独的时候,你能显示你的输出吗 -
它以“and”中的第一个字母分隔。 “一”