R 在与另一列中预先指定的字符串匹配后,删除一列中的滞后文本

R 在与另一列中预先指定的字符串匹配后,删除一列中的滞后文本,r,R,我以为我面前有一个简单的任务,结果我被难倒了。假设我有以下数据框: JobDF <- data.frame (occupation = c("Frank is a teacher for preschoolers","Jane is a doctor, and a good one","John cooks part-time"), abridged = c("a teacher", "and a&

我以为我面前有一个简单的任务,结果我被难倒了。假设我有以下数据框:

JobDF <- data.frame (occupation  = c("Frank is a teacher for preschoolers","Jane is a doctor, and a good one","John cooks part-time"), abridged = c("a teacher", "and a","cooks part"))
我不认为我可以使用sub或grepl,因为我的数据中没有规则模式,或者b删除不会在规则指定的数字位置发生在%中,将产生完全匹配的列。有人知道我该如何着手解决这个问题吗?提前谢谢你

您可以使用stringr::str_remove,它是矢量化的:

JobDF$occupation <- stringr::str_remove(JobDF$occupation, 
                    sprintf('(?<=%s).*', JobDF$abridged))

JobDF

#               occupation   abridged
#1      Frank is a teacher  a teacher
#2 Jane is a doctor, and a      and a
#3         John cooks part cooks part
您可以使用stringr::str_remove,它是矢量化的:

JobDF$occupation <- stringr::str_remove(JobDF$occupation, 
                    sprintf('(?<=%s).*', JobDF$abridged))

JobDF

#               occupation   abridged
#1      Frank is a teacher  a teacher
#2 Jane is a doctor, and a      and a
#3         John cooks part cooks part
我们可以使用tidyverse方法

我们可以使用tidyverse方法


你也可以使用它,它可能没有R的伟大贡献者之前提出的那样出色。它听起来可能有点冗长,但这是我第一次使用stru-locate,我真的很喜欢它:

library(dplyr)
library(stringr)
library(purrr)

map2(JobDF$occupation, JobDF$abridged, ~ str_locate(.x, .y)) %>%
  exec(rbind, !!!.) %>% 
  as_tibble() %>%
  bind_cols(JobDF) %>%
  rowwise() %>%
  mutate(occupation = map_chr(end, ~ str_sub(occupation, 1L, .x))) %>%
  select(-c(1, 2))


# A tibble: 3 x 2
# Rowwise: 
  occupation              abridged  
  <chr>                   <chr>     
1 Frank is a teacher      a teacher 
2 Jane is a doctor, and a and a     
3 John cooks part         cooks part

你也可以使用它,它可能没有R的伟大贡献者之前提出的那样出色。它听起来可能有点冗长,但这是我第一次使用stru-locate,我真的很喜欢它:

library(dplyr)
library(stringr)
library(purrr)

map2(JobDF$occupation, JobDF$abridged, ~ str_locate(.x, .y)) %>%
  exec(rbind, !!!.) %>% 
  as_tibble() %>%
  bind_cols(JobDF) %>%
  rowwise() %>%
  mutate(occupation = map_chr(end, ~ str_sub(occupation, 1L, .x))) %>%
  select(-c(1, 2))


# A tibble: 3 x 2
# Rowwise: 
  occupation              abridged  
  <chr>                   <chr>     
1 Frank is a teacher      a teacher 
2 Jane is a doctor, and a and a     
3 John cooks part         cooks part

感谢您提供多种解决方案!这两种方法在我的数据集中都非常有效,我当然也学会了一些使用R的新方法。再次感谢!感谢您提供多种解决方案!这两种方法在我的数据集中都非常有效,我当然也学会了一些使用R的新方法。再次感谢!非常感谢你。你说得很对。我正在努力解决行绑定未命名向量的问题,但没有任何可用性。但您的解决方案使我的代码不那么复杂。只需快速注释%>%bind_colsJobDF%>%mutateoccupation=str_subocupation,1,end也应该可以,因为str_sub是矢量化的数据,我明白。使用rbind,可以使用多种方法,即方法“rbind”,因此它将根据传入对象的类触发,即,如果输入是矩阵rbind,则将调用矩阵rbind。非常感谢。我真的不想占用你这么多时间。但我真的很感激。我也认为这是一个杰出的贡献!谢谢你的帮助。我很高兴你也能运用一项新技能。非常感谢。你说得很对。我正在努力解决行绑定未命名向量的问题,但没有任何可用性。但您的解决方案使我的代码不那么复杂。只需快速注释%>%bind_colsJobDF%>%mutateoccupation=str_subocupation,1,end也应该可以,因为str_sub是矢量化的数据,我明白。使用rbind,可以使用多种方法,即方法“rbind”,因此它将根据传入对象的类触发,即,如果输入是矩阵rbind,则将调用矩阵rbind。非常感谢。我真的不想占用你这么多时间。但我真的很感激。我也认为这是一个杰出的贡献!谢谢你的帮助。我很高兴你也能运用一项新技能。这是一个非常简洁的解决方案!谢谢你给我展示这个方法,akrun。这是一个非常简洁的解决方案!谢谢你给我展示这个方法,阿克伦。
library(dplyr)
library(stringr)
library(purrr)

map2(JobDF$occupation, JobDF$abridged, ~ str_locate(.x, .y)) %>%
  exec(rbind, !!!.) %>% 
  as_tibble() %>%
  bind_cols(JobDF) %>%
  rowwise() %>%
  mutate(occupation = map_chr(end, ~ str_sub(occupation, 1L, .x))) %>%
  select(-c(1, 2))


# A tibble: 3 x 2
# Rowwise: 
  occupation              abridged  
  <chr>                   <chr>     
1 Frank is a teacher      a teacher 
2 Jane is a doctor, and a and a     
3 John cooks part         cooks part
map2(JobDF$occupation, JobDF$abridged, ~ str_locate(.x, .y) %>% as_tibble) %>%
  bind_rows() %>%
  bind_cols(JobDF) %>%
  mutate(occupation = str_sub(occupation, 1L, end)) %>%
  select(-c(1, 2))