[R] Conditional mean for groups, new variables
arun
smartpink111 at yahoo.com
Mon Jun 2 02:37:53 CEST 2014
Hi,
May be this helps:
set.seed(42)
rev1 <- data.frame(SCHOOLID=sample(LETTERS[1:4],20,replace=TRUE), matrix(sample(25, 20*5,replace=TRUE), ncol=5, dimnames=list(NULL, c("MATH", "AGE", "STO2Q01", "BFMJ", "BMMJ"))),stringsAsFactors=FALSE)
res1 <- aggregate(rev1[,-1], list(SCHOOLID=rev1[,1]), mean,na.rm=TRUE)
res1
#if you need to change the names
res2 <- setNames(aggregate(rev1[,-1], list(SCHOOLID=rev1[,1]), mean,na.rm=TRUE), c("SCHOOLID", paste(colnames(rev1)[-1], "MEAN",sep="_")))
res2
A.K.
Hello! I have a problem, I want to calculate conditional mean for my dataset. First, I attach it:
rev<-read.csv("MATH1.csv", header=T, sep=";", dec=",")
attach(rev)
I have 650000 observations (test score) and 36000 groups (schoolid)
I need to calculate the mean for every group (schoolid) for the all my variables (MATH, AGE, ST02Q01,BFMJ,BMMJ. Actually, I have 34 varables, I just don't want to list them here) and then to create new variables for obtained new columns, because I want to estimate a new regression for the new obtained average values.
The following method is not appropriate for me, because it gives me in result a table with schoolid and the average for one variables, and I don't know how to extract the MATH coulmn with average values from the table with results to the worklist separately(environment).
aggregate( MATH~SCHOOLID, rev, mean)
How can I solve this problem? Thank for help!
More information about the R-help
mailing list