excel - Calculating AVERAGEIF in R by factor level -


in calculating percentiles factor using ave() in r, asked how calculate percentiles within ave() function. task finished, i'm faced more difficult task.

take following data:

districtname            building name   x2.yr.avg       thirty          seventy ionia public schools    emerson         -0.337464323    -0.196387489    -0.046524185 ionia public schools    jefferson       -0.318673587    -0.196387489    -0.046524185 ionia public schools    ionia middle    -0.290854669    -0.196387489    -0.046524185 ionia public schools    ionia middle    -0.288202752    -0.196387489    -0.046524185 ionia public schools    twin rivers el  -0.23426755     -0.196387489    -0.046524185 ionia public schools    r.b. boyce el   -0.202319963    -0.196387489    -0.046524185 ionia public schools    twin rivers el  -0.142995221    -0.196387489    -0.046524185 ionia public schools    emerson         -0.141620372    -0.196387489    -0.046524185 ionia public schools    jefferson       -0.141407078    -0.196387489    -0.046524185 ionia public schools    r.b. boyce el   -0.115530249    -0.196387489    -0.046524185 ionia public schools    ionia middle    -0.111449269    -0.196387489    -0.046524185 ionia public schools    twin rivers el  -0.054918339    -0.196387489    -0.046524185 ionia public schools    jefferson       -0.045591501    -0.196387489    -0.046524185 ionia public schools    a.a. rather     0.002251298     -0.196387489    -0.046524185 ionia public schools    r.b. boyce el   0.020669633     -0.196387489    -0.046524185 ionia public schools    emerson         0.065064968     -0.196387489    -0.046524185 ionia public schools    a.a. rather     0.182776319     -0.196387489    -0.046524185 

what i'm trying akin averageif function in excel. in excel, can =averageif(c2:c18, "<-.196387489"), spits out average value -0.278630474. need in r allows me following: want create new variables average value of: 1) values of x2.yr.avg smaller value of thirty 2) values larger value of seventy

the catch need able perform operation in large data frame 722 levels factor districtname. in step calculating percentiles, used ave() function create percentiles according desired factor follows:

    mathgap$thirty<-ave(mathgap$x2.yr.avg, mathgap$districtname,         fun= function(x) quantile(x, 0.3)) 

and

    mathgap$seventy<-ave(mathgap$x2.yr.avg, mathgap$districtname,         fun= function(x) quantile(x, 0.7)) 

is there way akin averageif within ave() operation repeated each value of districtname independently of others? i.e, ionia public schools should have average value x2.yr.avg less -0.196387489 , x2.yr.avg greater -0.046524185, , want able perform same function districts using respective values x2.yr.avg, thirty, , seventy.

if confusing, apologies.

here's solution using dplyr:

mathgap %>%   group_by(districtname) %>%   mutate(meanlt30 = mean(x2.yr.avg[x2.yr.avg < thirty]),     meantgt70 = mean(x2.yr.avg[x2.yr.avg > seventy])) 

Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -