ewaters
2009-Oct-15 03:55 UTC
[R] performing function on lists where each element is a data frame
I have browsed the archive, and played around with a whole bunch of ways of approaching the problem, and I cannot get the answer that I want. I have a list with 2000 elements. Each element is a data frame with 2 columns. As an example, see element 1999 below. [[1999]] H N 6 0.54 26 10 0.31 13 11 0.12 34 13 0.00 38 4 0.59 37 16 0.44 9 19 0.08 13 14 0.64 39 8 0.75 12 24 0.42 26 9 0.61 111 20 0.22 9 Now, I want to caluculate the weighted mean for H in each list element, with N providing the weights. I can do this fine for an individual list element:> weighted.mean(list[[1999]]$H,list[[1999]]$N)[1] 0.4456403 or even:> weighted.mean(list[[i]]$H,list[[i]]$N)[1] 0.5992683 but when I try and apply it in a lopp to give me back the weighted mean for each list element, I get errors:> list_blah<-matrix(1:2000) > for(i in 1:2000){+ list_blah[i]=weighted.mean(list[[i]]$H,list[[i]]$N)} Error in list[[i]] : subscript out of bounds I am clearly not understanding a basic syntax issue, and I am not overly skilled at using loops. I have also tried using lapply instead of a for loop, with the same problem. Asking for list[i] instead of list[[i]] gives NA results, as would be expected. Can anyone shed light? weighted.mean -- View this message in context: http://www.nabble.com/performing-function-on-lists-where-each-element-is-a-data-frame-tp25902745p25902745.html Sent from the R help mailing list archive at Nabble.com.
Phil Spector
2009-Oct-15 07:53 UTC
[R] performing function on lists where each element is a data frame
Are you saying that sapply(xx,function(x)weighted.mean(x$H,x$N)) assuming the list of data frames was called xx, doesn't give the result you want? Could you please elaborate as to why sapply doesn't give the correct result? - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Wed, 14 Oct 2009, ewaters wrote:> > I have browsed the archive, and played around with a whole bunch of ways of > approaching the problem, and I cannot get the answer that I want. > > I have a list with 2000 elements. Each element is a data frame with 2 > columns. As an example, see element 1999 below. > > [[1999]] > H N > 6 0.54 26 > 10 0.31 13 > 11 0.12 34 > 13 0.00 38 > 4 0.59 37 > 16 0.44 9 > 19 0.08 13 > 14 0.64 39 > 8 0.75 12 > 24 0.42 26 > 9 0.61 111 > 20 0.22 9 > > Now, I want to caluculate the weighted mean for H in each list element, with > N providing the weights. I can do this fine for an individual list element: > >> weighted.mean(list[[1999]]$H,list[[1999]]$N) > [1] 0.4456403 > > or even: >> weighted.mean(list[[i]]$H,list[[i]]$N) > [1] 0.5992683 > > but when I try and apply it in a lopp to give me back the weighted mean for > each list element, I get errors: > >> list_blah<-matrix(1:2000) >> for(i in 1:2000){ > + list_blah[i]=weighted.mean(list[[i]]$H,list[[i]]$N)} > Error in list[[i]] : subscript out of bounds > > I am clearly not understanding a basic syntax issue, and I am not overly > skilled at using loops. I have also tried using lapply instead of a for > loop, with the same problem. > > Asking for list[i] instead of list[[i]] gives NA results, as would be > expected. > > Can anyone shed light? > > > weighted.mean > > -- > View this message in context: http://www.nabble.com/performing-function-on-lists-where-each-element-is-a-data-frame-tp25902745p25902745.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. >