Message-ID: <501AB6D8.9050206@uke.de>
Date: 2012-08-02T17:20:24Z
From: Eik Vettorazzi
Subject: apply function over same column of all objects in a list
In-Reply-To: <1343916561536-4638876.post@n4.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,
>
> 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.
>
> ______________________________________________
> 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.
>
--
Eik Vettorazzi
Institut f?r Medizinische Biometrie und Epidemiologie
Universit?tsklinikum Hamburg-Eppendorf
Martinistr. 52
20246 Hamburg
T ++49/40/7410-58243
F ++49/40/7410-57790
--
Pflichtangaben gem?? Gesetz ?ber elektronische Handelsregister und Genossenschaftsregister sowie das Unternehmensregister (EHUG):
Universit?tsklinikum Hamburg-Eppendorf; K?rperschaft des ?ffentlichen Rechts; Gerichtsstand: Hamburg
Vorstandsmitglieder: Prof. Dr. Guido Sauter (Vertreter des Vorsitzenden), Dr. Alexander Kirstein, Joachim Pr?l?, Prof. Dr. Dr. Uwe Koch-Gromus