Dear list, I have this dataframe V1 V2 F1 1 A 2 0 2 A 3 0 3 A 4 1 4 B 3 0 5 B 2 1 6 C 6 0 7 C 2 0 8 C 6 0 and would like to calculate a new column with mean-values, following this rule 1. If F1 = 0 calculate the mean from V2 for each factor in V1. 2. If F1 = 1, then F1_mean = 0 So, the new DF should look like this V1 V2 F1 F1_mean 1 A 2 0 2.5 2 A 3 0 2.5 3 A 4 1 0.0 4 B 3 0 3.0 5 B 2 1 0.0 6 C 6 0 7.0 7 C 2 0 7.0 8 C 6 0 7.0 Thank you for any help! Patrick Hausmann
Hi r-help-bounces at r-project.org napsal dne 13.11.2007 10:59:09:> Dear list, > > I have this dataframe > > V1 V2 F1 > 1 A 2 0 > 2 A 3 0 > 3 A 4 1 > 4 B 3 0 > 5 B 2 1 > 6 C 6 0 > 7 C 2 0 > 8 C 6 0 > > and would like to calculate a new column > with mean-values, following this rule > > 1. If F1 = 0 calculate the mean from V2 > for each factor in V1. > > 2. If F1 = 1, then F1_mean = 0 > > So, the new DF should look like this > > V1 V2 F1 F1_mean > 1 A 2 0 2.5 > 2 A 3 0 2.5 > 3 A 4 1 0.0 > 4 B 3 0 3.0 > 5 B 2 1 0.0 > 6 C 6 0 7.0 > 7 C 2 0 7.0 > 8 C 6 0 7.0I would use ave for computing mean for combination of V1 and F1 and then I put all values of F1_mean for which F1 is 1 to 0 test$F1_mean <- ave(test$V2, test$V1, factor(test$F1), FUN = mean) test$F1_mean[test$F1==1] <- 0 Regards Petr> > Thank you for any help! > > Patrick Hausmann > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
One possibility is: a<-" V1 V2 F1 + 1 A 2 0 + 2 A 3 0 + 3 A 4 1 + 4 B 3 0 + 5 B 2 1 + 6 C 6 0 + 7 C 2 0 + 8 C 6 0 + " b<-read.table(textConnection(a),header=TRUE) f<-rep(0,dim(b)[1]) f[b$F1==0] <- ave(b$V2[b$F1==0],b$V1[b$F1==0]) cbind(b,f) V1 V2 F1 f 1 A 2 0 2.500000 2 A 3 0 2.500000 3 A 4 1 0.000000 4 B 3 0 3.000000 5 B 2 1 0.000000 6 C 6 0 4.666667 7 C 2 0 4.666667 8 C 6 0 4.666667 --- c18g at uni-bremen.de wrote:> Dear list, > > I have this dataframe > > V1 V2 F1 > 1 A 2 0 > 2 A 3 0 > 3 A 4 1 > 4 B 3 0 > 5 B 2 1 > 6 C 6 0 > 7 C 2 0 > 8 C 6 0 > > and would like to calculate a new column > with mean-values, following this rule > > 1. If F1 = 0 calculate the mean from V2 > for each factor in V1. > > 2. If F1 = 1, then F1_mean = 0 > > So, the new DF should look like this > > V1 V2 F1 F1_mean > 1 A 2 0 2.5 > 2 A 3 0 2.5 > 3 A 4 1 0.0 > 4 B 3 0 3.0 > 5 B 2 1 0.0 > 6 C 6 0 7.0 > 7 C 2 0 7.0 > 8 C 6 0 7.0 > > Thank you for any help! > > Patrick Hausmann > > ______________________________________________ > 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. >