我有一个dataframe列,它有一个字符串,可能包含几个空格.我想在第一次出现关键字(即样本数据中的fruit_key)后,在空格中使用tidyr中的separate(或类似的东西),这样我就可以将一列分隔为两列.

Sample Data

df <- structure(list(fruit = c("Apple Orange Pineapple", "Plum Good Watermelon", 
"Plum Good Kiwi", "Plum Good Plum Good", "Cantaloupe Melon", "Blueberry Blackberry Cobbler", 
"Peach Pie Apple Pie")), class = "data.frame", row.names = c(NA, 
-7L))

fruit_key <- c("Apple", "Plum Good", "Cantaloupe", "Blueberry", "Peach Pie")

Expected Output

                         fruit   Delicious                Tasty
1       Apple Orange Pineapple       Apple     Orange Pineapple
2         Plum Good Watermelon   Plum Good           Watermelon
3               Plum Good Kiwi   Plum Good                 Kiwi
4          Plum Good Plum Good   Plum Good            Plum Good
5             Cantaloupe Melon  Cantaloupe                Melon
6 Blueberry Blackberry Cobbler   Blueberry   Blackberry Cobbler
7          Peach Pie Apple Pie   Peach Pie            Apple Pie

我可以将关键字后面的部分(separate)放入正确的列(即Tasty),但无法将实际关键字返回到另一列(即Delicious).我try 了多次修改正则表达式,但始终无法得到正确的输出.

library(tidyr)

separate(df, fruit,
 c("Delicious", "Tasty"),
 sep = paste(fruit_key, collapse = "|"),
 extra = "merge",
 remove = FALSE
)

#                         fruit Delicious               Tasty
#1       Apple Orange Pineapple              Orange Pineapple
#2         Plum Good Watermelon                    Watermelon
#3               Plum Good Kiwi                          Kiwi
#4          Plum Good Plum Good                     Plum Good
#5             Cantaloupe Melon                         Melon
#6 Blueberry Blackberry Cobbler            Blackberry Cobbler
#7          Peach Pie Apple Pie                     Apple Pie

我知道我可以使用str_extractstr_remove(如下所示),但我想用separate这样的东西在一个函数/步骤中完成它.

library(tidyverse)

df %>%
  mutate(Delicious = str_extract(fruit, paste(fruit_key, collapse = "|")),
         Tasty = str_remove(fruit, paste(fruit_key, collapse = "|")))

推荐答案

如果我们需要将separatesep结合使用,那么创建一个regex lookaround-"(?<=<fruit_key>) ",即在果_关键字后面的空格处拆分,并将collapse|(str_c)组合成一个字符串

library(dplyr)
library(tidyr)
library(stringr)
df %>% 
   separate(fruit, into = c("Delicious", "Tasty"), 
     sep = str_c(sprintf("(?<=%s) ", fruit_key), collapse = "|"), 
         extra = "merge", remove = FALSE)

-输出

                       fruit  Delicious              Tasty
1       Apple Orange Pineapple      Apple   Orange Pineapple
2         Plum Good Watermelon  Plum Good         Watermelon
3               Plum Good Kiwi  Plum Good               Kiwi
4          Plum Good Plum Good  Plum Good          Plum Good
5             Cantaloupe Melon Cantaloupe              Melon
6 Blueberry Blackberry Cobbler  Blueberry Blackberry Cobbler
7          Peach Pie Apple Pie  Peach Pie          Apple Pie

R相关问答推荐

按条件计算观察次数

如果行和列名以相同的开头,将矩阵值设置为0

查找满足SpatRaster中条件的单元格位置

使用gggrassure减少地块之间的空间

如何根据R中其他列的值有条件地从列中提取数据?

使用sf或terra的LINESTRAING的累积长度

在另存为PNG之前隐藏htmlwidget绘图元素

在RStudio中堆叠条形图和折线图

如何在R库GoogleDrive中完全删除预先授权的Google帐户?

有没有办法使用ggText,<;Sub>;&;<;sup>;将上标和下标添加到同一元素?

列名具有特殊字符时的循环回归

随机森林的带Shap值的蜂群图

我如何使用tidyselect来传递一个符号数组,比如Pivot_Long?

在具有多个响应变量的比例堆叠条形图上方添加总计

以不同于绘图中元素的方式对GG图图例进行排序

如何提取R中其他字符串和数字之间的字符串?

删除字符串R中的重复项

如何预测原始数据集并将值添加到原始数据集中

是否有可能从边界中找到一个点值?

如何将图例文本添加到图例符号中