intersect multi-dimensional arrays in R -
I am interested in hiding many lists of identifiers, the number of overlap between the lists ('RK') vs. ' T ') I have a vague idea that sapply is the way to go but I am still stuck after searching and reading tutorial
rk1 < -. List ("YH_sensitive_933", "CS_sensitive_1294", "YH_sensitive_944" ("YH_sensitive_1299", "Y_Sensitive_1299", "YY_sensitive_922", "YY_sensitive_922", "YH_sensitive_952", "YA_sensitive_949") • rk2 & lt; - list ("YH_sensitive_944", " The list ("YH_sensitive_944", "JB_persistent_1224", " JB_persistent_1224 ") t3 & lt; - list (" CS_sensitive_1299 "," YY_sensitive_922 "," YH_sensitive_944 ") T4 & lt; - list (" YH_sensitive_952 "," YA_sensitive_949 ")
edit : I thought that it would be best to group two lists of lists and as a suggestion, / p>
F But I am starting an R and really appreciate some guidance on using looping and guidance functionals. But I only look at intersections for RK1 (but not R2, which should be 1 2 3 2)
lapply / sapply
F & lt; - List (T1, T2, T3, T4) G & lt; - List (rk1, rk2) res & lt; - Use .call (`c`, setNames (lapply (G, function (.y) setNames (sapply (F,` intersect`, .y), paste0 ("t", 1: 4)), paste 0 ("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 reset $ 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" Race $ rk2.t1 # [ [1]] # [1] "YH_sensitive_944" intersection (RK 2, T-1) # [[1]] # [1] "YH_sensitive_944"
or mapply < / Code> (Comment of @Recread scripting from the original idea)
dat1 & lt; - wide .grid (ls (pattern mapp (dat1 [, 1]), mget (dat1 [, 2])) [1] [1] [1] [1] 1]] # [[1]] # [1 ] "YH_sensitive_933" # [[2]] # [1] "CS_sensitive_1294" # [[3]] # [1] "YH_sensitive_944"
update
< Change the code> res to matrix
mat 1 <- do.call (cbind, lapply (missing (res, unlisted), `length of & lt; -` max (sapply (race, length)))) mat1 # rk1.t1 rk1.t2 rk1.t3 # [1,] "YH_sensitive_933" "YH_sensitive_944" "CS_sensitive_1299" # [2,] "[4]" "," "YH_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 # RK2.t3 rk2.t4 # [1,] "CS_sensitive_1299" "YH_s "YY_sensitive_922" "YH_sensitive_949" # [3,] "YH_sensitive_944" NA
Update2
If you have length
Get the output in the matrix,
resL < - sapply (res, length) m1 & lt; - Matrix (Resl, nrow = 2, byrow = TRUE, dimnames = list (paste0 ("rk", 1: 2), paste 0 ("T", 1: 4))) M1 # T1 T2 T3T 4 # RK1 3 2 3 2 # RK2 1 2 3 2
Comments
Post a Comment