我有以下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
V1
中的字符串数总是小于3.我需要一个R代码,它告诉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
答案 0 :(得分:1)
试试这个:
mapply(function(x,y) sum(x %in% y),
strsplit(df$V1,",", fixed=TRUE),strsplit(df$V2,",", fixed=TRUE))
#[1] 1 2 3
数据强>
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")