Re: [R] weighted averages for two variables

2012-05-19 Thread mpostje
It works now :) thank you! -- View this message in context: http://r.789695.n4.nabble.com/weighted-averages-for-two-variables-tp4630589p4630610.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://s

Re: [R] weighted averages for two variables

2012-05-19 Thread mpostje
This is the output that i got > dput( head(allspecies, 20) ) structure(list(Year = c(1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1980L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L), Quarter = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,

Re: [R] weighted averages for two variables

2012-05-19 Thread Rui Barradas
Hello, Thanks. The revised 'by' I had posted works with me. An alternative, more complicated, is using split/sapply. sapply(split(allspecies, allspecies$Year), function(x) weighted.mean(x$Depth, x$CPUE)) Rui Barradas __ R-help@r-project.org

Re: [R] weighted averages for two variables

2012-05-19 Thread Rui Barradas
Hello, Post a data example, it will give us something to work on, and give you better answers. dput( head(allspecies, 20) ) Then copy the output of this and paste it in your post. As for your problem, considering it's description, and untested (no data), maybe result <- by(allspecies, all

[R] weighted averages for two variables

2012-05-19 Thread mpostje
Hi R users, I have a dataset with multiple variables and i'm trying to weigh average depths for fish species per year by their abundance (CPUE. I have tried the weighted.mean function but as i have two columns for the x value the lenghts differ with the w (CPUE). How do I solve this problem? So f