Corinne Lapare
2012-Dec-13 14:31 UTC
[R] How do I make a loop to extract a column from multiple lists and then bind them together to make a new matrix?
Hi! I am new to looping and R in general; and I have sent waaaay to much time on this one problem and am about a hair away from doing it manually for the next two days. So, there is a package that while calculating the statistic creates lists (that look like matrices) in the background. Each item (there are 10 items) has one of these ‘matrix looking list’ that I need to extract data from. The list has 5 rows that represent 5 groups, and 8 columns. I need to extract 3 of the columns (‘Lo Score’=[,2], ‘Hi Score=[,3]’, and ‘Mean’=[,7]) for each of the items. I then want to turn the extracted data into 3 matrices (‘Lo Score’, ‘Hi Score’, and ‘Mean’) where the rows are the 5 groups and the columns are items 1-10. This is how I can create the mean matrix by hand. “MDD.mean.s10” is the matrix I want in the end. (notice the first bracket after $results is the only part that changes 1-10 (to represent the 10 items) and the last bracket is [,7] to represent the mean located in column 7)> m.1a <- MC_MDD.noNA$results[[1]][[2]][,7]> m.2b <- MC_MDD.noNA$results[[2]][[2]][,7]> m.3c <- MC_MDD.noNA$results[[3]][[2]][,7]> m.4d <- MC_MDD.noNA$results[[4]][[2]][,7]> m.5e <- MC_MDD.noNA$results[[5]][[2]][,7]> m.6f <- MC_MDD.noNA$results[[6]][[2]][,7]> m.7g <- MC_MDD.noNA$results[[7]][[2]][,7]> m.8h <- MC_MDD.noNA$results[[8]][[2]][,7]> m.9i <- MC_MDD.noNA$results[[9]][[2]][,7]> m.10j <- MC_MDD.noNA$results[[10]][[2]][,7]> MDD.mean.s10 <- cbind(m.1a, m.2b, m.3c, m.4d, m.5e, m.6f, m.7g, m.8h,m.9i, m.10j)>> MDD.mean.s10m.1a m.2b m.3c m.4d m.5e m.6f m.7g m.8h m.9i m.10j [1,] 0.8707865 0.7393939 0.7769231 0.7591241 0.8533333 0.7925926 0.8258065 0.8410596 0.8843931 0.5638298 [2,] 0.8323353 0.7302632 0.5913978 0.5868263 0.6923077 0.6182796 0.6964286 0.6839080 0.7911392 0.3212121 [3,] 0.8726115 0.7159763 0.7117647 0.6163522 0.7987805 0.7105263 0.7613636 0.7674419 0.8034682 0.4011299 [4,] 0.9024390 0.7894737 0.7795276 0.6530612 0.8593750 0.7112676 0.8672566 0.8629032 0.9152542 0.4834437 [5,] 0.9861111 0.9102564 0.8452381 0.8160920 0.9726027 0.8658537 0.8352941 0.9342105 0.9466667 0.6454545 But I can’t do this by hand every time, as this comes up over and over and over again in multiple lists. I have figured out how to loop this procedure and name the vector as it goes along:> for(i in 1:10){+ assign(paste("m", i, sep = ""), MC_MDD.noNA$results[[i]][[2]][,7]) + }>>> m1[1] 0.8707865 0.8323353 0.8726115 0.9024390 0.9861111> m2[1] 0.7393939 0.7302632 0.7159763 0.7894737 0.9102564> m3[1] 0.7769231 0.5913978 0.7117647 0.7795276 0.8452381> m4[1] 0.7591241 0.5868263 0.6163522 0.6530612 0.8160920> m5[1] 0.8533333 0.6923077 0.7987805 0.8593750 0.9726027> m6[1] 0.7925926 0.6182796 0.7105263 0.7112676 0.8658537> m7[1] 0.8258065 0.6964286 0.7613636 0.8672566 0.8352941> m8[1] 0.8410596 0.6839080 0.7674419 0.8629032 0.9342105> m9[1] 0.8843931 0.7911392 0.8034682 0.9152542 0.9466667> m10[1] 0.5638298 0.3212121 0.4011299 0.4834437 0.6454545 Now here where I get stuck… how do I cbind these vectors without typing it out expliciity? ie. mean.MDD <- cbind(m1,m2,m3,m4,m5,m6,m7,m8,m9,10) Everything I have tried keeps overwriting the data instead of building a matrix. Basically I, start with a matrix (5x10) of zeros. Then I wind up with a few values in the beginning, but the rest is still zeros. Example of terrible code:> fo <- matrix(0,5,10)> colnames(fo) <- paste('f', 1:10, sep = "")> fof1 f2 f3 f4 f5 f6 f7 f8 f9 f10 [1,] 0 0 0 0 0 0 0 0 0 0 [2,] 0 0 0 0 0 0 0 0 0 0 [3,] 0 0 0 0 0 0 0 0 0 0 [4,] 0 0 0 0 0 0 0 0 0 0 [5,] 0 0 0 0 0 0 0 0 0 0> for(i in 1:10){+ fo <- assign(paste("f", i, sep = ""), MC_MDD.noNA$results[[i]][[2]][,7]) + }> fo[1] 0.5638298 0.3212121 0.4011299 0.4834437 0.6454545> fo <- matrix(0,5,10)> colnames(fo) <- paste('f', 1:10, sep = "")> fof1 f2 f3 f4 f5 f6 f7 f8 f9 f10 [1,] 0 0 0 0 0 0 0 0 0 0 [2,] 0 0 0 0 0 0 0 0 0 0 [3,] 0 0 0 0 0 0 0 0 0 0 [4,] 0 0 0 0 0 0 0 0 0 0 [5,] 0 0 0 0 0 0 0 0 0 0> for(i in 1:10){+ fo <- cbind(assign(paste("f", i, sep = ""), MC_MDD.noNA$results[[i]][[2]][,7])) + }> fo[,1] [1,] 0.5638298 [2,] 0.3212121 [3,] 0.4011299 [4,] 0.4834437 [5,] 0.6454545 Thanks for your help in advance!!! (c: [[alternative HTML version deleted]]
Adams, Jean
2012-Dec-13 19:38 UTC
[R] How do I make a loop to extract a column from multiple lists and then bind them together to make a new matrix?
Try this ... MDD.mean.s10 <- sapply(MC_MDD.noNA$results, function(x) x[[2]][, 7]) Jean On Thu, Dec 13, 2012 at 8:31 AM, Corinne Lapare <corinnelapare@gmail.com>wrote:> Hi! I am new to looping and R in general; and I have sent waaaay to much > time on this one problem and am about a hair away from doing it manually > for the next two days. > > > > So, there is a package that while calculating the statistic creates lists > (that look like matrices) in the background. Each item (there are 10 > items) has one of these ‘matrix looking list’ that I need to extract data > from. The list has 5 rows that represent 5 groups, and 8 columns. I need > to extract 3 of the columns (‘Lo Score’=[,2], ‘Hi Score=[,3]’, and > ‘Mean’=[,7]) for each of the items. I then want to turn the extracted data > into 3 matrices (‘Lo Score’, ‘Hi Score’, and ‘Mean’) where the rows are the > 5 groups and the columns are items 1-10. > > > > This is how I can create the mean matrix by hand. “MDD.mean.s10” is the > matrix I want in the end. (notice the first bracket after $results is the > only part that changes 1-10 (to represent the 10 items) and the last > bracket is [,7] to represent the mean located in column 7) > > > m.1a <- MC_MDD.noNA$results[[1]][[2]][,7] > > > m.2b <- MC_MDD.noNA$results[[2]][[2]][,7] > > > m.3c <- MC_MDD.noNA$results[[3]][[2]][,7] > > > m.4d <- MC_MDD.noNA$results[[4]][[2]][,7] > > > m.5e <- MC_MDD.noNA$results[[5]][[2]][,7] > > > m.6f <- MC_MDD.noNA$results[[6]][[2]][,7] > > > m.7g <- MC_MDD.noNA$results[[7]][[2]][,7] > > > m.8h <- MC_MDD.noNA$results[[8]][[2]][,7] > > > m.9i <- MC_MDD.noNA$results[[9]][[2]][,7] > > > m.10j <- MC_MDD.noNA$results[[10]][[2]][,7] > > > MDD.mean.s10 <- cbind(m.1a, m.2b, m.3c, m.4d, m.5e, m.6f, m.7g, m.8h, > m.9i, m.10j) > > > > > > MDD.mean.s10 > > m.1a m.2b m.3c m.4d m.5e m.6f m.7g > m.8h m.9i m.10j > > [1,] 0.8707865 0.7393939 0.7769231 0.7591241 0.8533333 0.7925926 0.8258065 > 0.8410596 0.8843931 0.5638298 > > [2,] 0.8323353 0.7302632 0.5913978 0.5868263 0.6923077 0.6182796 0.6964286 > 0.6839080 0.7911392 0.3212121 > > [3,] 0.8726115 0.7159763 0.7117647 0.6163522 0.7987805 0.7105263 0.7613636 > 0.7674419 0.8034682 0.4011299 > > [4,] 0.9024390 0.7894737 0.7795276 0.6530612 0.8593750 0.7112676 0.8672566 > 0.8629032 0.9152542 0.4834437 > > [5,] 0.9861111 0.9102564 0.8452381 0.8160920 0.9726027 0.8658537 0.8352941 > 0.9342105 0.9466667 0.6454545 > > > > > > But I can’t do this by hand every time, as this comes up over and over and > over again in multiple lists. > > I have figured out how to loop this procedure and name the vector as it > goes along: > > > for(i in 1:10){ > > + assign(paste("m", i, sep = ""), MC_MDD.noNA$results[[i]][[2]][,7]) > > + } > > > > > > > > > m1 > > [1] 0.8707865 0.8323353 0.8726115 0.9024390 0.9861111 > > > m2 > > [1] 0.7393939 0.7302632 0.7159763 0.7894737 0.9102564 > > > m3 > > [1] 0.7769231 0.5913978 0.7117647 0.7795276 0.8452381 > > > m4 > > [1] 0.7591241 0.5868263 0.6163522 0.6530612 0.8160920 > > > m5 > > [1] 0.8533333 0.6923077 0.7987805 0.8593750 0.9726027 > > > m6 > > [1] 0.7925926 0.6182796 0.7105263 0.7112676 0.8658537 > > > m7 > > [1] 0.8258065 0.6964286 0.7613636 0.8672566 0.8352941 > > > m8 > > [1] 0.8410596 0.6839080 0.7674419 0.8629032 0.9342105 > > > m9 > > [1] 0.8843931 0.7911392 0.8034682 0.9152542 0.9466667 > > > m10 > > [1] 0.5638298 0.3212121 0.4011299 0.4834437 0.6454545 > > > > > > Now here where I get stuck… how do I cbind these vectors without typing it > out expliciity? ie. mean.MDD <- cbind(m1,m2,m3,m4,m5,m6,m7,m8,m9,10) > > > > Everything I have tried keeps overwriting the data instead of building a > matrix. Basically I, start with a matrix (5x10) of zeros. Then I wind up > with a few values in the beginning, but the rest is still zeros. > > Example of terrible code: > > > fo <- matrix(0,5,10) > > > colnames(fo) <- paste('f', 1:10, sep = "") > > > fo > > f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 > > [1,] 0 0 0 0 0 0 0 0 0 0 > > [2,] 0 0 0 0 0 0 0 0 0 0 > > [3,] 0 0 0 0 0 0 0 0 0 0 > > [4,] 0 0 0 0 0 0 0 0 0 0 > > [5,] 0 0 0 0 0 0 0 0 0 0 > > > for(i in 1:10){ > > + fo <- assign(paste("f", i, sep = ""), > MC_MDD.noNA$results[[i]][[2]][,7]) > > + } > > > fo > > [1] 0.5638298 0.3212121 0.4011299 0.4834437 0.6454545 > > > > > fo <- matrix(0,5,10) > > > colnames(fo) <- paste('f', 1:10, sep = "") > > > fo > > f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 > > [1,] 0 0 0 0 0 0 0 0 0 0 > > [2,] 0 0 0 0 0 0 0 0 0 0 > > [3,] 0 0 0 0 0 0 0 0 0 0 > > [4,] 0 0 0 0 0 0 0 0 0 0 > > [5,] 0 0 0 0 0 0 0 0 0 0 > > > for(i in 1:10){ > > + fo <- cbind(assign(paste("f", i, sep = ""), > MC_MDD.noNA$results[[i]][[2]][,7])) > > + } > > > fo > > [,1] > > [1,] 0.5638298 > > [2,] 0.3212121 > > [3,] 0.4011299 > > [4,] 0.4834437 > > [5,] 0.6454545 > > > > > > > > > > Thanks for your help in advance!!! (c: > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >[[alternative HTML version deleted]]