Match two lists, one with partial rows and the other with a full row, return the whole row if a match

Match two lists in R, one with partial rows and the other with a full row, return the whole row if they match. Return only unique matches (once).

So let's say I have a CSV file, and each line has a long line (long list). Then I cut the line with substr, and then I delete any duplicate lines using unique ones. Then I want to compare a long list of df12 lines with a unique short list of df14 , and if there are unique matches in a partial line search ( df14 vs df12 ) then return the whole line from df12 .

This is df12 (long list of lines)

  [1] I like stackoverflow very much today [2] I like stackoverflow much today [3] I dont like stackoverflow very much today [4] I dont like you! [5] What? df13<-substr(df12, start=0, stop=30) 

This is df13 (shortened strings are not unique)

 [1] I like stacko [2] I like stacko [3] I dont like s [4] I dont like y [5] What? df14<-unique(df13) 

This is df14 (abbreviated lines are unique lines after applying a unique method)

  [1] I like stacko [2] I dont like s [3] I dont like y [4] What? 

This is the result I want at the end

  [1] I like stackoverflow very much today [2] I dont like stackoverflow very much today [3] I dont like you! [4] What? 
+5
source share
1 answer

This is one way to match each short line in df14 with all possible matches in df12 and display them, including a short line as an index in the list, to see which one matches df12:

 df1 <- c('I like stackoverflow very much today', 'I like stackoverflow much today', 'I dont like stackoverflow very much today', 'I dont like you!', 'What?') df2 <- c('I like stacko', 'I dont like s', 'I dont like y', 'What?') sapply(df2, function(x) df1[grepl(x, df1)]) $`I like stacko` [1] "I like stackoverflow very much today" "I like stackoverflow much today" $`I dont like s` [1] "I dont like stackoverflow very much today" $`I dont like y` [1] "I dont like you!" $`What?` [1] "What?" 
+3
source

Source: https://habr.com/ru/post/1240965/


All Articles