1

我有兴趣交叉多个标识符列表,制作一个包含列表对('rk'与't')之间重叠数量的表。我有一个模糊的想法,即 sapply 是要走的路,但在搜索和阅读教程后我仍然卡住了。

rk1 <- list("YH_sensitive_933","CS_sensitive_1294","YH_sensitive_944","JB_persistent_1224","CS_sensitive_1299","YY_sensitive_922", "YH_sensitive_952","YA_sensitive_949")   
rk2 <- list("YH_sensitive_944","JB_persistent_1224","CS_sensitive_1299","YY_sensitive_922", "YH_sensitive_952","YA_sensitive_949")
t1 <- list("YH_sensitive_933","CS_sensitive_1294","YH_sensitive_944")
t2 <- list("YH_sensitive_944","JB_persistent_1224")
t3 <- list("CS_sensitive_1299","YY_sensitive_922","YH_sensitive_944")
t4 <- list("YH_sensitive_952","YA_sensitive_949")   

编辑:我认为最好将两个列表列表分组并按照建议尝试 sapply/mapply

F <- list(t1,t2,t3,t4)
G <- list(rk1,rk2)
> sapply(mapply(intersect,F,G), length)
[1] 3 2 3 2

但我是 R 初学者,非常感谢有关循环和使用应用函数的一些指导。但我只看到 rk1 的交叉点(但没有看到 rk2,应该是 1 2 3 2)

4

2 回答 2

1

使用lapply/sapply

 F <- list(t1, t2, t3, t4)
 G <- list(rk1, rk2)  

res <- do.call(`c`,setNames(lapply(G, function(.y) 
         setNames(sapply(F, `intersect`, .y), paste0("t",1:4))), paste0("rk",1:2)))

 sapply(res, length)
 #rk1.t1 rk1.t2 rk1.t3 rk1.t4 rk2.t1 rk2.t2 rk2.t3 rk2.t4 
 #    3      2      3      2      1      2      3      2 

res$rk1.t1
#[[1]]
#[1] "YH_sensitive_933"

#[[2]]
#[1] "CS_sensitive_1294"

#[[3]]
#[1] "YH_sensitive_944"


intersect(rk1,t1)
#[[1]]
#[1] "YH_sensitive_933"

#[[2]]
#[1] "CS_sensitive_1294"

#[[3]]
#[1] "YH_sensitive_944"

res$rk2.t1
# [[1]]
#[1] "YH_sensitive_944"

 intersect(rk2, t1)
 #[[1]]
 #[1] "YH_sensitive_944"

或者您可以使用mapply(@Richard Scriven 评论中的基本想法)

 dat1 <- expand.grid(ls(pattern="^rk"), ls(pattern="^t"),stringsAsFactors=F)
 res1 <- mapply(intersect, mget(dat1[,1]), mget(dat1[,2]))

 res1[[1]]
 #[[1]]
 #[1] "YH_sensitive_933"

 #[[2]]
 #[1] "CS_sensitive_1294"

 #[[3]]
 #[1] "YH_sensitive_944"

更新

将 转换res为矩阵

  mat1 <- do.call(cbind,lapply(lapply(res, unlist),
                     `length<-`, max(sapply(res, length))))


  mat1
  #       rk1.t1              rk1.t2               rk1.t3             
  #[1,] "YH_sensitive_933"  "YH_sensitive_944"   "CS_sensitive_1299"
  #[2,] "CS_sensitive_1294" "JB_persistent_1224" "YY_sensitive_922" 
  #[3,] "YH_sensitive_944"  NA                   "YH_sensitive_944" 
  #     rk1.t4             rk2.t1             rk2.t2              
  #[1,] "YH_sensitive_952" "YH_sensitive_944" "YH_sensitive_944"  
  #[2,] "YA_sensitive_949" NA                 "JB_persistent_1224"
  #[3,] NA                 NA                 NA                  
  #      rk2.t3              rk2.t4            
  #[1,] "CS_sensitive_1299" "YH_sensitive_952"
  #[2,] "YY_sensitive_922"  "YA_sensitive_949"
  #[3,] "YH_sensitive_944"  NA 

更新2

如果你需要得到length矩阵的输出,

  resL <- sapply(res,length)
  m1 <-  matrix(resL, nrow=2, byrow=TRUE,
          dimnames=list(paste0("rk", 1:2), paste0("t",1:4)))

  m1
  #   t1 t2 t3 t4
  #rk1  3  2  3  2
  #rk2  1  2  3  2
于 2014-10-17T18:04:58.110 回答
0

成对重叠:

ListOfVectors<- list(v1,v2,...)

pwOvlaps<-sapply(seq_len(length(ListOfVectors)), function(x) sapply(seq_len(length(ListOfVectors)), function(y) length(intersect(unlist(ListOfVectors[x]), unlist(ListOfVectors[y])))))

colnames(pwOvlaps)<-names(ListOfVectors)
rownames(pwOvlaps)<-names(ListOfVectors)

ps:来这里是为了寻找真正的多个交叉路口的解决方案,例如,

intersect(a,b,c,d,...,z)-> 
list(intersect(a,b), 
intersect(a,b,c), 
...)
于 2021-06-25T09:46:57.000 回答