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
Post a Comment