[R] apply function over same column of all objects in a list

R. Michael Weylandt michael.weylandt at gmail.com
Thu Aug 2 00:30:00 CEST 2012


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.



More information about the R-help mailing list