[R] Mean: category wise within a data frame
arun
smartpink111 at yahoo.com
Tue Dec 24 18:21:47 CET 2013
HI,
May be this helps:
Please use ?dput() to show the example dataset
dat1 <- read.table(text="Categories Variable Frequencies
1 0.009 1003
1 0.867 1200
1 0.567 987
1 0.765 134
1 0.445 890
2 0.007 899
2 0.768 707
2 0.789 865
2 0.544 678
2 0.987 889",sep="",header=TRUE)
library(plyr)
res <- ddply(transform(dat1,NewCol=Variable*Frequencies),.(Categories),summarize, Avg=mean(NewCol/sum(Frequencies)))
A.K.
On Tuesday, December 24, 2013 10:43 AM, Nandini Jayakumar <nandini_dec6 at hotmail.com> wrote:
Hello all
I have a table a sample of which is as follows:
Categories
Variable (x)
Frequencies
1
1
1
1
1
0.009
0.867
0.567
0.765
0.445
1003
1200
987
134
890
2
2
2
2
2
0.007
0.768
0.789
0.544
0.987
899
707
865
678
889
3
3
3
3
3
0.898
0.887
0.560
0.098
0.987
544
677
934
467
876
40
40
40
40
40
0.786
0.342
0.456
0.987
0.123
843
987
675
467
223
Basically I have 40 categories and each category has several hundred variables. I want to calculate the average per category, that is variable * frequency/Summation of frequencies. I want to do it for each category separately. Since i have many categories i do not want to use the subset() function 40 times. Is it possible to do it within a single data frame?
Really appreciate any help. Thank you.
[[alternative HTML version deleted]]
______________________________________________
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.
More information about the R-help
mailing list