簡體   English   中英

跨數據幀R多列的每一行的gsub

[英]gsub across each row of multiple columns of dataframe R

我有一個示例數據框,如下所示:

dat1 <- data.frame(Col1= c("a woman's hat 9 in. long", "ABC company news", "P.F.Chang's house", "this would weigh 90 lbs."),
Col2= c("9 in.", "ABC", "P.F.Chang's", "90 lbs."),
stringsAsFactors=F)

dat1
                      Col1        Col2
1 a woman's hat 9 in. long       9 in.
2         ABC company news         ABC
3        P.F.Chang's house P.F.Chang's
4 this would weigh 90 lbs.     90 lbs.

我想刪除與數據框的col2匹配的col1的一部分。 因此,我希望結果如下:

                Col1        Col2
1 a woman's hat long       9 in.
2       company news         ABC
3              house P.F.Chang's
4   this would weigh     90 lbs.

我嘗試了gsub(dat1$col2, '', dat1$col1) 但是,這只會使用dat1 $ col2的第一個元素作為模式。

感謝任何輸入以獲得結果

謝謝!

我們可以將'Col2'中的元素paste在一起,在gsub中將其用作pattern ,在替換中使用'' ,並用trimws刪除前導/滯后空格。

dat1$col1 <- trimws(gsub(paste(dat1$Col2, collapse='|'), 
                '', dat1$Col1))
dat1$col1
#[1] "a woman's hat  long" "company news"        "house"               "this would weigh"   

我們也可以使用stri_replace

library(stringi)
stri_trim(stri_replace(dat1$Col1, fixed=dat1$Col2, ""))
#[1] "a woman's hat  long" "company news"        "house"               "this would weigh"   

一個更緊湊的方法是

library(qdap)
with(dat1, mgsub(Col2, '', Col1))
#[1] "a woman's hat long" "company news"       "house"              "this would weigh"  

嘗試使用stringr程序包,然后進行Nicola建議的編輯- fixed(dat1$col2)

library(stringr)
str_replace(dat1$Col1, fixed(dat1$Col2), "")

"a woman's hat  long" " company news"       " house"              "this would weigh "  
> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM