r - Count the number of strings in one column common with strings in other column -


i have following data.frame:

v1    | v2 ------+------------ a,b,c | x,y,z,w,t,a c,d   | d,z,c,t a,b,d | a,f,t,b,d 

where count of strings in v1 under 3. need r code tells number of strings common v2:

v1    | v2          | count ------+-------------+------ a,b,c | x,y,z,w,t,a | 1 c,d   | d,z,c,t     | 2 a,b,d | a,f,t,b,d   | 3 

try this:

mapply(function(x,y) sum(x %in% y),    strsplit(df$v1,",", fixed=true),strsplit(df$v2,",", fixed=true)) #[1] 1 2 3 

data

df<-structure(list(v1 = c("a,b,c", "c,d", "a,b,d"), v2 = c("x,y,z,w,t,a",  "d,z,c,t", "a,f,t,b,d")), .names = c("v1", "v2"), row.names = c(na,  -3l), class = "data.frame") 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -