[英]R remove words from sentences in dataframe
我有一個 dataframe 有兩列,每列包含句子,我想從另一個中減去一個。 我不知何故無法輕松找到執行以下操作的方法:
> c1 <- c("A short story","Not so short")
> c2 <- c("A short", "Not so")
> data.frame(c1, c2)
這應該給出 c1 - c2 的結果
"story","short"
任何想法都是有幫助的。
我們可以使用矢量化的str_remove
library(stringr)
library(dplyr)
df1 %>%
mutate(c3 = str_remove_all(c1, c2))
c1 c2 c3
#1 A short story A short story
#2 Not so short Not so short
你可以這樣做:
c1 <- c("A short story","Not so short")
c2 <- c("A short", "Not so")
dat <- data.frame(c1, c2)
dat$c3 <- purrr::map2_chr(c1, c2, ~ trimws(gsub(.y, "", .x)))
dat
#> c1 c2 c3
#> 1 A short story A short story
#> 2 Not so short Not so short
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.