user3635284
user3635284

Reputation: 513

R: Apply function to every row of dataframe containing strings

Sorry for this, it's probably a duplicate but I'm having a bad time with the apply function. I would like to apply a function to every row of a dataset where the function will do an operation between two strings. Example:

(imagine a dataframe with two colums: t1 & t2) "this is", "this is a test" "my head will explode", "i like my head"

I have a function that takes two strings and returns the number of words in common something like:

commonwords <- function(s1,s2) {
  return (length(intersect(strsplit(s1,split=" ")[[1]],strsplit(s2,split=" ")[[1]])))
}

(Note I haven't pasted the function I just quick typed it to give you an indea so syntax could be wrong!)

I just need a way to apply that function to every row of the dataframe returning a new column with the number of words in common. This can then be extended to any other operations between two strings.

Thanks a lot for the help I think this will be quick :)

Luis.

Upvotes: 0

Views: 782

Answers (1)

agstudy
agstudy

Reputation: 121578

First please , take the time to create a reproducible example and clean code.

Since you want to apply an operation between 2 variables, you should use mapply here.

xx <- 
data.frame(S1=c( "this is","my head will explode"),
           S2=c("this is a test", "i like my head"))

 mapply(commonwords,xx[,'S1'],xx[,'S2'])
         this is my head will explode 
               2                    2 

Where commonwords is:

commonwords <- function(s1,s2) {
  length(intersect(strsplit(s1,split=" ")[[1]],
                   strsplit(s2,split=" ")[[1]]))
}

Upvotes: 2

Related Questions