using loop to create a new list from previous list in r -
i executing function (computemodules{bipartite}) yields different results every time run. want iterate execution 100 times. function's output consists of list several slots. have extract matrix 1 of these slots (@modules) , subset obtain rows i'm interested in. later using these rows select positions of vector. in end, want obtain 100 lists many sublists rows subsetted in each iteration.
simulated @modules:
n1 <- structure(c(1, 1, 1, 2, 2, 2, 2, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 2, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 4, 0, 5, 0, 0, 0, 0, 5, 0, 6, 0, 0, 0, 0, 6, 0, 7, 0, 0, 0, 0, 7, 0, 8, 0, 0, 0, 0, 8, 0, 9, 0, 0, 9, 0, 0, 0, 10, 0, 0, 10, 0, 0, 0, 11, 0, 0, 0, 11, 0, 12, 0, 0, 12, 0, 0, 0, 0, 13, 0, 0, 0, 0, 13, 14, 0, 0, 14, 0, 0, 0, 0, 15, 0, 0, 0, 0, 15, 16, 0, 0, 16, 0, 0, 0, 17, 0, 0, 17, 0, 0, 0, 0, 18, 0, 0, 0, 0, 18, 0, 19, 0, 0, 0, 0, 19, 20, 0, 0, 20, 0, 0, 0, 0, 21, 0, 0, 21, 0, 0, 0, 22, 0, 0, 22, 0, 0, 0, 23, 0, 0, 0, 23, 0, 0, 24, 0, 0, 24, 0, 0, 0, 25, 0, 0, 25, 0, 0, 0, 26, 0, 0, 0, 26, 0, 0, 27, 0, 0, 27, 0, 0, 0, 28, 0, 0, 0, 28, 0), .dim = c(7l, 30l)) n2 <- structure(c(1, 1, 2, 2, 2, 2, 2, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 3, 0, 0, 3, 0, 0, 0, 4, 0, 0, 4, 0, 0, 0, 5, 0, 0, 5, 0, 0, 0, 6, 0, 0, 6, 0, 0, 0, 7, 0, 0, 7, 0, 0, 0, 8, 0, 0, 8, 0, 0, 0, 9, 0, 0, 0, 0, 9, 0, 10, 0, 0, 0, 0, 10, 0, 11, 0, 0, 0, 11, 0, 12, 0, 12, 0, 0, 0, 0, 0, 13, 0, 0, 13, 0, 0, 14, 0, 14, 0, 0, 0, 0, 0, 15, 0, 0, 15, 0, 0, 16, 0, 16, 0, 0, 0, 0, 17, 0, 17, 0, 0, 0, 0, 0, 18, 0, 0, 18, 0, 0, 0, 19, 0, 0, 19, 0, 0, 20, 0, 20, 0, 0, 0, 0, 0, 21, 0, 0, 0, 0, 21, 0, 22, 0, 0, 0, 0, 22, 0, 23, 0, 0, 0, 23, 0, 0, 24, 0, 0, 0, 0, 24, 0, 25, 0, 0, 0, 0, 25, 0, 26, 0, 0, 0, 26, 0, 0, 27, 0, 0, 0, 0, 27, 0, 28, 0, 0, 0, 28, 0), .dim = c(7l, 30l))
my code:
e <- 1:30 nr=2 n=list(n1, n2); comp2=list(); u=list(); m=list() for(i in 1:nr){ m[[i]] <- n[[i]][,-c(1,2, (ncol(n[[i]])-5):ncol(n[[i]]))] # select columns of interest comp2[[i]] <- which(n[[i]][,1]==2) # subset values of column 1 (total 5) for(j in 1:length(comp2[[i]])){ # here want create 2xlength(comp2[[i]]) list u[[i]] <- e[unlist(m[[i]][comp2[[i]][j],])] # create list u selecting values of e } }
i obtain:
> u [1]] > e[unlist(m[[1]][comp2[[1]][1],])] [1] 12 14 16 17 20 > e[unlist(m[[1]][comp2[[1]][2],])] [1] 2 9 10 21 22 > e[unlist(m[[1]][comp2[[1]][3],])] [1] 11 > e[unlist(m[[1]][comp2[[1]][4],])] [1] 1 3 4 5 6 7 8 13 15 18 19 [[2]] > e[unlist(m[[2]][comp2[[2]][1],])] [1] 12 14 16 17 20 > e[unlist(m[[2]][comp2[[2]][2],])] [1] integer(0) > e[unlist(m[[2]][comp2[[2]][3],])] [1] 1 3 4 5 6 7 8 13 15 18 19 > e[unlist(m[[2]][comp2[[2]][4],])] [1] 11 > e[unlist(m[[2]][comp2[[2]][5],])] [1] 2 9 10 21 22
but code overwrites output of each iteration , keeps last 1 each list:
> u [[1]] [1] 1 3 4 5 6 7 8 13 15 18 19 [[2]] [1] 2 9 10 21 22
how can desired list of lists?
edit: real data example
data <- structure(list(mc8 = c(1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 0l, 0l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l), mc9 = c(1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 0l, 0l, 0l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l), gc9 = c(1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l), gc8 = c(1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l), gc7 = c(1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l), gc6 = c(1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l,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 1l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l), p8 = c(0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l), p9 = c(0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l), p10 = c(0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l)), .names = c("mc8", "mc9", "gc9", "gc8", "gc7", "gc6", "gc5", "gc4", "gc3", "gc2", "gc1", "cc", "cp1", "cp2", "cp3", "cp4", "cp5", "cp6", "cp7", "cp8", "cp9", "s9", "s8", "s7", "s6", "s5", "s4", "s3", "s2", "s1", "p1", "p2", "p3", "p4", "p5", "p6", "p7", "p8", "p9", "p10"), class = "data.frame", row.names = c(na, -40l))
adapted code:
pl <- 40 # number of columns deleted in tmp@modules eox <- unlist(dimnames(data)) # names of rows , columns l <- vector("list", 2l) # lists (i in 1:length(l)) { tmp <- computemodules(data) tmp <- tmp@modules ss1 <- which(tmp[,1]==1) # subsetting module 1 tmp <- tmp[,-c(1,2,(ncol(tmp)-pl+1):ncol(tmp))] # delete columns of no interest (j in 1:length(ss1)) { l[[i]] <- eox[tmp[ss1[j],]] # rows of tmp meet subsetting conditions; use each row select position in eox. then, create many sublists rows in tmp3 , assign l[[i]] }
everything works fine last line of code not yield desired output , last loop overwrites previous ones. perhaps desired output obtained more directly?
perhaps pseudocode?
l <- vector("list", 100l) (i in 1:length(l)) { tmp <- computemodules(...) # specific arguments tmp <- tmp@modules # slot want tmp <- subset(tmp, ...) # subsetting requirements l[[i]] <- tmp # put piece/answer want list } res <- unlist(l)
or maybe need loop on l
again , different stuff. alternatively, if did this: l[[i]] <- computemodules(...)
list of lists.
edit: small version data
library('bipartite') data(small1976) l <- vector("list", 1l) (i in 1:length(l)) { tmp <- computemodules(small1976) # specific arguments tmp <- tmp@modules # slot want tmp <- subset(tmp, ...) # subsetting requirements l[[i]] <- tmp # put piece/answer want list }
without subsetting, tmp
numeric matrix. need it?
edit 2: if right, overthinking subsetting process.
library("bipartite") eox <- unlist(dimnames(data)) pl <- 40 l <- vector("list", 1l) (i in 1:length(l)) { tmp <- computemodules(data, deep = true) tmp <- tmp@modules # slot want tmp <- tmp[,-c(1,2,(ncol(tmp)-pl+1):ncol(tmp))] # delete columns of no interest ss1 <- which(tmp[,1]==1) # subsetting module 1 l[[i]] <- eox[ss1] # put piece/answer want list }
gives, l
:
[[1]] [1] "1" "2" "4"
but there must random fitting going on, different answers on different runs.
Comments
Post a Comment