Extracting and Merge in R -


below list have:

v1        v2  zealous   jane pretty    may smart     kate place     china 

below data have:

name     source jj       kate has brother kl       may lives in china. 

i make use list , data have extract data matches using r.

below output get:

name    source                words   comments jj      kate has brother    kate    smart kl      may lives in china    may     pretty kl      may lives in china    china   place 

thank you.

we can use str_extract extract words df2 , match 'nm1' 'v2' column of 'df1' index 'v1' column of 'df1'

library(stringr) nm1 <- str_extract(df2$source, paste(df1$v2, collapse='|')) df2$words <- df1$v1[match(nm1, df1$v2)] df2 #  name              source  words #1   jj  kate has brother  smart #2   kl may lives in china. pretty 

update

for updated dataset ('df1'), can use str_extract_all extract multiple words in list, stack convert data.frame, merge df2 , match 'values' column 'v2' 'df1' create 'comments' column.

 nm1 <- str_extract_all(df2$source, paste(df1$v2, collapse='|'))  d1 <- stack(setnames(nm1, df2$name))  df2n <- merge(df2, d1, by.x='name', by.y='ind')  df2n$comments <- df1$v1[match(df2n$values, df1$v2)]  colnames(df2n)[3] <- 'words'  df2n  #  name              source words comments  #1   jj  kate has brother  kate    smart  #2   kl may lives in china.   may   pretty  #3   kl may lives in china. china    place 

data

df1 <- structure(list(v1 = c("zealous", "pretty", "smart"),  v2 = c("jane",  "may", "kate")), .names = c("v1", "v2"), class = "data.frame", row.names = c(na, -3l))  df2 <- structure(list(name = c("jj", "kl"),  source = c("kate has brother",  "may lives in china.")), .names = c("name", "source"), class = "data.frame", row.names = c(na, -2l)) 

updated data

df1 <- structure(list(v1 = c("zealous", "pretty", "smart", "place"),  v2 = c("jane", "may", "kate", "china")), .names = c("v1",  "v2"), class = "data.frame", row.names = c(na, -4l)) 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -