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, allspecies$Year, function(x) 
weighted.mean(x$Depth, x$CPUE))



Also, note that I've renamed the result, 'data' is an R function.

Hope this helps,

Rui Barradas

__
R-help@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.


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 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.


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, 1L, 1L, 1L, 1L, 1L, 1L
), latitude = c(54.9, 54.4833, 54.4833, 53.9167, 53.9167, 54.7833, 
58.8667, 58.25, 58.25, 57.9, 57.9, 57.9667, 56.5333, 56.5333, 
56.5333, 56.5333, 54.7167, 54.7167, 54.7167, 54.45), longitude = c(6.1333, 
7.55, 7.55, 0.9, 0.9, 1.0333, -2.3833, -3.15, -3.15, -3.3667, 
-3.3667, -2.8333, -2.2, -2.2, -2.2, -2.2, 7., 7., 7., 
7.55), Depth = c(33, 24, 24, 49, 49, 55, 76, 69, 69, 57, 57, 
73, 46, 46, 46, 46, 20, 20, 20, 20), Area = c(6L, 6L, 6L, 4L, 
4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 6L, 6L, 6L, 6L
), Species = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c(Agonus
cataphractus, 
Amblyraja radiata, Anarhichas lupus, Arnoglossus laterna, 
Buglossidium luteum, Echiichthys vipera, Eutrigla gurnardus, 
Gadus morhua, Glyptocephalus cynoglossus, Hippoglossoides
platessoides, 
Lepidorhombus whiffiagonis, Limanda limanda, Lophius piscatorius, 
Melanogrammus aeglefinus, Merlangius merlangus, Merluccius merluccius, 
Microstomus kitt, Molva molva, Pleuronectes platessa, Pollachius
virens, 
Scyliorhinus canicula, Solea solea, Squalus acanthias, 
Trisopterus esmarkii, Trisopterus luscus, Trisopterus minutus
), class = factor), CPUE = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), .Names = c(Year, Quarter, 
latitude, longitude, Depth, Area, Species, CPUE), row.names =
15:34, class = data.frame)



--
View this message in context: 
http://r.789695.n4.nabble.com/weighted-averages-for-two-variables-tp4630589p4630604.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


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://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.