intersect multi-dimensional arrays in R -



intersect multi-dimensional arrays in R -

i interested in intersecting multiple lists of identifiers, making table number of overlaps between pairs of lists ('rk' vs 't'). have vague thought sapply way go still stuck after searching , reading tutorials.

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")

edit: thought maybe it'd best grouping 2 lists of lists , seek sapply/mapply suggested

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

but i'm r beginner , appreciate guidance on looping , using apply functionals. see intersections rk1 (but not rk2, should 1 2 3 2)

using 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"

or utilize mapply (basic thought @richard scriven's comment)

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" update

to convert res matrix

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 update2

if need length output in matrix,

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

arrays r sapply mapply

Comments

Popular posts from this blog

java Multi query from Mysql using netbeans -

c# - DotNetZip fails with "stream does not support seek operations" -

c++ - StartServiceCtrlDispatcher don't can access 1063 error -