Hello. Please forgive me if this problem has already been posted (and solved) by someone else ... I can't find it anywhere though it seems so very basic. Here it is: I have a list comprised of several matrices, each of which has two columns.> list[[1]] [,1] [,2] [1,] 1 3 [2,] 2 4 [[2]] [,1] [,2] [1,] 5 7 [2,] 6 8 [[3]] [,1] [,2] [1,] 9 11 [2,] 10 12 I would like R to give me the highest value (max) of each matrice's 2nd column, and also the max of all those individual highest values. I have tried matrix.max <- for (i in 1:3) { mapply ( max, list [[i]] [2] ) } for each column's max and total.max <- for (i in 1:3) { max(mapply ( max, list [[i]] [2] )) } for the total max, but neither works (result: NULL). What am I doing wrong? Thanks a lot, Gail -- View this message in context: http://r.789695.n4.nabble.com/apply-function-over-same-column-of-all-objects-in-a-list-tp4638681.html Sent from the R help mailing list archive at Nabble.com.
Thank you!!! But I realise I've simplified my data to the point that your solution doesn't actually work -- not your fault, mine! My list is actually more complicated than what I presented it to be; it's not composed of numerical matrices but of lists, each being composed of 7 columns, the first two of each (the ones that I am interested in, x and y) being numerical. This is what my list really looks like (truncated):> summary(mylist)Length Class Mode moh6 7 density list moh7 7 density list moh8 7 density list ... etc.> summary(mylist[[3]]) # I've taken number 3 as an example, but they're all > the sameLength Class Mode x 512 -none- numeric y 512 -none- numeric bw 1 -none- numeric n 1 -none- numeric call 2 -none- call data.name 1 -none- character has.na 1 -none- logical Any suggestion how to get the max of column y for each sub-list (moh6, moh7, moh8 etc.), and to the max of all these individual maxes? By the way, lapply(list1,FUN=function(x)x[,2][which.max(x[,2])]) gives me the individual maxes (for a list composed of numerical matrices), but how do I get to the overall max? Many thanks ... -- View this message in context: http://r.789695.n4.nabble.com/apply-function-over-same-column-of-all-objects-in-a-list-tp4638681p4638705.html Sent from the R help mailing list archive at Nabble.com.
R. Michael Weylandt
2012-Aug-01 22:30 UTC
[R] apply function over same column of all objects in a list
On Wed, Aug 1, 2012 at 8:43 AM, gail <ortoga-r at yahoo.de> wrote:> Hello. Please forgive me if this problem has already been posted (and solved) > by someone else ... I can't find it anywhere though it seems so very basic. > Here it is: > > I have a list comprised of several matrices, each of which has two columns. > >> list > [[1]] > [,1] [,2] > [1,] 1 3 > [2,] 2 4 > > [[2]] > [,1] [,2] > [1,] 5 7 > [2,] 6 8 > > [[3]] > [,1] [,2] > [1,] 9 11 > [2,] 10 12 > > I would like R to give me the highest value (max) of each matrice's 2nd > column, and also the max of all those individual highest values. I have > tried > > matrix.max <- for (i in 1:3) { > mapply ( max, list [[i]] [2] ) > } > > for each column's max and > > total.max <- for (i in 1:3) { > max(mapply ( max, list [[i]] [2] )) > } >I think what you just want is to put the "get second column" part within the function that you apply() [Actually lapply() since you have a list] -- to put it another way, note that "get the second column" can be considered part of the operation you perform on each list element, rather than defining the thing to which you apply the max() function. Something like this results: lapply(list, function(x) max(x[,2])) This will go over the list and apply the function "get the max of the second column" Best, Michael> for the total max, but neither works (result: NULL). What am I doing wrong? > > Thanks a lot, > Gail > > > > -- > View this message in context: http://r.789695.n4.nabble.com/apply-function-over-same-column-of-all-objects-in-a-list-tp4638681.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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.
arun kirshna [via R]
2012-Aug-02 05:49 UTC
[R] apply function over same column of all objects in a list
HI, I tested the code for another set of data with added complexity. Seems to be working fine. list1<-list(data.frame(x=1:6,y=c(4,5,3,2,1,8),bw=c(4,18,12,3,4,9))) list2<-list(data.frame(x=c(1,2,18,16,15),y=c(4,5,9,2,1),bw=c(4,18,22,3,4))) list3<-list(data.frame(x=c(4,6,9),y=c(8,24,12),bw=c(14,31,36))) list4<-list(list1,list2,list3) #individual cases lapply(list4[[1]][[1]],FUN=function(x) x[which.max(x)]) lapply(list4[[2]][[1]],FUN=function(x) x[which.max(x)]) lapply(list4[[3]][[1]],FUN=function(x) x[which.max(x)]) #Whole set flatlist <- function(mylist){ lapply(rapply(mylist, enquote, how="unlist"), eval) } list5<-flatlist(list4) dat6<-data.frame(key=names(list5),dat5) dat6 aggregate(value~key,data=dat6,max) #key value #1 bw 36 #2 x 18 #3 y 24 A.K. ______________________________________ If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/apply-function-over-same-column-of-all-objects-in-a-list-tp4638681p4638829.html This email was sent by arun kirshna (via Nabble) To receive all replies by email, subscribe to this discussion: http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=subscribe_by_code&node=4638681&code=ci1oZWxwQHItcHJvamVjdC5vcmd8NDYzODY4MXwtNzg0MjM1NTA4 [[alternative HTML version deleted]]
Arun, I see you've defined dat5 in your later message; however the same applies as to the above: the code doesn't work if the list contains non-numerical elements. Gail -- View this message in context: http://r.789695.n4.nabble.com/apply-function-over-same-column-of-all-objects-in-a-list-tp4638681p4638859.html Sent from the R help mailing list archive at Nabble.com.
Reasonably Related Threads
- again, a question between R and C++
- Creating object referant from argument name
- assigning from multiple return values
- Assigning a vector to every element of a list.
- substitute "x" for "pattern" in a list, while preservign list "structure". lapply, gsub, list...?