Skip to content

apply function over same column of all objects in a list

15 messages · R. Michael Weylandt, gail, arun kirshna [via R] +1 more

#
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):
Length Class   Mode
moh6  7      density list
moh7  7      density list
moh8  7      density list
...
etc.
Length 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.
#
I'm a little confused on your data structure -- can you use dput() as
described here [1] to give a small reproducible example?

http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

Michael
On Wed, Aug 1, 2012 at 10:38 AM, gail <ortoga-r at yahoo.de> wrote:
Possibly

max(unlist(list1))

if I understand you correctly.
#
Michael, here is the data I am working with. I've shortened it without, I
trust, changing its nature. I am not using any package other than what is
pre-installed. Many thanks, Gail
list

(structure(list(

x = c(379.455895016957, 380.116796110287, 
380.777697203618, 381.438598296949, 382.099499390279, 382.76040048361, 
383.421301576941, 384.082202670271, 384.743103763602, 385.404004856933, 
386.064905950264, 386.725807043594), 

y = c(7.66474496376218e-06, 
6.43440919959401e-06, 5.36894293042274e-06, 4.45194069157015e-06, 
3.66781132570054e-06, 3.00181397191447e-06, 2.45773671502347e-06, 
2.00342333333225e-06, 1.62212866305772e-06, 1.3043460449395e-06, 
1.04141214324248e-06, 8.26430099678339e-07), 

bw = 9.02854660253144, 

n = 1102L,

call = density.default(x = read.table(paste(locuteur, 
        i, sep = ""))$V1[read.table(paste(locuteur, i, sep = ""))$V1 != 
        0]),

data.name = c("read.table(paste(locuteur, i, sep =
\"\"))$V1[read.table(paste(locuteur, ", 
    "    i, sep = \"\"))$V1 != 0]"), has.na = FALSE),

.Names = c("x", 
"y", "bw", "n", "call", "data.name", "has.na"),

class = "density"),

structure(list(x = c(372.576546447762, 373.241257067042, 
    373.905967686322, 374.570678305602, 375.235388924882, 375.900099544162, 
    376.564810163442, 377.229520782722, 377.894231402002, 378.558942021282, 
    379.223652640562, 379.888363259842), y = c(1.60229850505427e-05, 
    1.30822330219271e-05, 1.05973406669671e-05, 8.51514876035057e-06, 
    6.78543779138952e-06, 5.41596769065147e-06, 4.28876937728122e-06, 
    3.36815237991786e-06, 2.62273082547721e-06, 2.02454969122432e-06, 
    1.54894049348427e-06, 1.18505840947482e-06), bw = 7.90353278894747, 
        n = 1409L, call = density.default(x = read.table(paste(locuteur, 
            i, sep = ""))$V1[read.table(paste(locuteur, i, sep = ""))$V1 != 
            0]), data.name = c("read.table(paste(locuteur, i, sep =
\"\"))$V1[read.table(paste(locuteur, ", 
        "    i, sep = \"\"))$V1 != 0]"), has.na = FALSE), .Names = c("x", 
    "y", "bw", "n", "call", "data.name", "has.na"), class = "density"),

    structure(list(x = c(383.473231160603, 384.169713215726, 
    384.866195270849, 385.562677325973, 386.259159381096, 386.955641436219, 
    387.652123491343, 388.348605546466, 389.045087601589, 389.741569656713, 
    390.438051711836, 391.134533766959), y = c(2.40798446492758e-06, 
    2.04205952412882e-06, 1.72782287870817e-06, 1.45399002130161e-06, 
    1.21665858527511e-06, 1.01213493683702e-06, 8.37503296444613e-07, 
    6.94344355906326e-07, 5.72200769661348e-07, 4.6862434236517e-07, 
    3.81353514437358e-07, 3.08309463721622e-07), bw = 10.0108886969865, 
        n = 1522L, call = density.default(x = read.table(paste(locuteur, 
            i, sep = ""))$V1[read.table(paste(locuteur, i, sep = ""))$V1 != 
            0]), data.name = c("read.table(paste(locuteur, i, sep =
\"\"))$V1[read.table(paste(locuteur, ", 
        "    i, sep = \"\"))$V1 != 0]"), has.na = FALSE), .Names = c("x", 
    "y", "bw", "n", "call", "data.name", "has.na"), class = "density"),

    NULL, NULL, NULL, NULL, NULL, NULL)




--
View this message in context: http://r.789695.n4.nabble.com/apply-function-over-same-column-of-all-objects-in-a-list-tp4638681p4638865.html
Sent from the R help mailing list archive at Nabble.com.
#
Hi,

my dataset is the result of the function "density" on another set of data.
It refers to that data in its variable "call", though since all the results
are actually reproduced (except that I've removed all rows bar 10), I am not
sure why R still needs it. But I've understood now why your code doesn't
work on my data: your sublists are still just dataframes, whereas my
sublists contain all sorts of vectors (2 numerical vectors and 5 other
things). I've changed your data to make it look a bit more like mine, and
you'll see that the code doesn't work. 

mango <- list(data.frame(coconut=1:6), y=c(4,5,3,2,1,8),
bw=c(4,18,12,3,4,9), nn=paste0("a",1:6))
banana <- list(data.frame(coconut=c(1,2,18,16,15)), y=c(4,5,9,2,1),
bw=c(4,18,22,3,4), nn=paste0("a",1:5))
pineapple <- list(data.frame(coconut=c(4,6,9)), y=c(8,24,12),
bw=c(14,31,36), nn=paste0("a",1:3))
list4 <- list(mango, banana, pineapple)

b <- list()

 for(i in 1:3){
 b[[i]] <- list()
 b[[i]] <- lapply (list4[[i]]$coconut, FUN=function(x) x[which.max(x)])
 }

b
summary( b)

What I want to end up with is the following:

           coconut
mango       6
banana     18
pineapple   9
(that's for the individual sublists)

and 
           coconut
                18
(that's for the list as a whole).

Hope this is becoming a bit clearer ...



--
View this message in context: http://r.789695.n4.nabble.com/apply-function-over-same-column-of-all-objects-in-a-list-tp4638681p4638876.html
Sent from the R help mailing list archive at Nabble.com.
#
Well but the whole point was to be able to plug in a single command and get
the result for all sublists. If I have to create a new list and type in each
sublist and the column I'm interested in, that completely defeats the
purpose, I might as well just ask for max(sublist$coconut) for each sublist,
that would be just as fast ...



--
View this message in context: http://r.789695.n4.nabble.com/apply-function-over-same-column-of-all-objects-in-a-list-tp4638681p4638888.html
Sent from the R help mailing list archive at Nabble.com.
#
I still try to figure out, what you are finally asking for, but maybe

res<-sapply(list4,function(x)max(x[[1]]$coconut))
names(res)<-c("mango", "banana", "pineapple")
res
max(res)

is worth a try?

Especially i did not get the point of doing something like
x[which.max(x)] because this is in any case just max(x)

if you would like to plug in different functions or want to select
different columns,

fun<-max # or min, median etc
sapply(lapply(list4,"[[",1),fun)

could be a start, where lapply(list4,"[[",1) extracts the first object
in each sublist - which is in your case a data.frame with just one
column ('coconut'), where 'fun' can be applied to. So to address
'coconut' explicitly, just nest this lapply approach:

sapply(lapply(lapply(list4,"[[",1),"[[","coconut"),fun)
#or use an anonymous extractor function
sapply(lapply(list4,function(x)x[[1]][["coconut"]]),fun)

cheers

Am 02.08.2012 16:09, schrieb gail:

  
    
#
Hi Arun,
if you name the data.frame in the list, e.g.
mango <- list( y=c(4,5,3,2,1,8), bw=c(4,18,12,3,4,9),
nn=paste0("a",1:6),target=data.frame(coconut=1:6))
banana <- list(target=data.frame(coconut=c(1,2,18,16,15)), y=c(4,5,9,2,1),
bw=c(4,18,22,3,4), nn=paste0("a",1:5))
pineapple <- list(target=data.frame(coconut=c(4,6,9)), y=c(8,24,12),
bw=c(14,31,36), nn=paste0("a",1:3))
list6<-list(mango,banana,pineapple)

then
sapply(lapply(list5,"[[","target"),max)

still works.

Regards

Am 02.08.2012 20:21, schrieb arun kirshna [via R]: