Using grepl in R to match string

十年热恋 提交于 2019-12-12 09:49:18

问题


I have a frame data "testData" as follows:

id     content
 1     I came from China
 2     I came from America
 3     I came from Canada
 4     I came from Japan
 5     I came from Mars

And I also have another frame data "addr" as follows:

id   addr
 1   America
 2   Canada
 3   China
 4   Japan

Then how can I use grepl, sapply or any other useful function in R to generate data into as follows:

id   content               addr
 1   I came from China     China
 2   I came from America   America
 3   I came from Canada    Canada
 4   I came from Japan     Japan
 5   I came from Mars      Mars

回答1:


This does the trick:

vec = addr$addr

testData$addr = apply(testData, 1, function(u){
    bool = sapply(vec, function(x) grepl(x, u[['content']]))
    if(any(bool)) vec[bool] else NA
})



回答2:


Looks like you just want to replicate the column and remove "I came from "

testData$addr <- gsub("I came from ", testData$content)



回答3:


Here is a crude solution using some tidyverse functions:

df1 <- read.table(text = "id     content
 1     'it is China'
 2     'She is in America now'
 3     'Canada is over there'
 4     'He comes from Japan'
 5     'I came from Mars'", header = TRUE, stringsAsFactors = FALSE)

df2 = read.table(text = "id   addr
 1   America
 2   Canada
 3   China
 4   Japan
 5   Mars", header = TRUE, stringsAsFactors = FALSE)

library(tidyverse)
crossing(df1, df2 %>% select(addr)) %>% # this creates a data frame of every possible content and add combination
  rowwise() %>% 
  filter(str_detect(content, add)) # str_detect is the same as grepl, though the arguments are reversed. This filters to only observations where addr is in content.

# A tibble: 5 x 3
     id content               addr   
  <int> <chr>                 <chr>  
1     1 it is China           China  
2     2 She is in America now America
3     3 Canada is over there  Canada 
4     4 He comes from Japan   Japan  
5     5 I came from Mars      Mars


来源:https://stackoverflow.com/questions/28230360/using-grepl-in-r-to-match-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!