4

Below is the script to generate a reproducible dataframe:

id <- c(1:20)
a <- as.numeric(round(runif(20,-40,40),2))
b <- as.numeric(round(a*1.4+60,2))
df <- as.data.frame(cbind(id, a, b))

i would like to calculate the mean of "b" under different condition for "a". for example, what is the mean of "b" when -40 =< a < 0; and what is the mean of "b" when 0=< a <=40.

Thank you very much!

cyrusjan
  • 637
  • 1
  • 8
  • 23

2 Answers2

4

Here's a quick data.table solution (assuming coef is a)

library(data.table)
setDT(df)[, .(MeanASmall = mean(b[-40 <= a & a < 0]),
              MeanABig = mean(b[0 <= a & a <= 40]))]
#    MeanASmall MeanABig
# 1:   33.96727    89.46

If a range is limited, you could do this quickly with base R too

sapply(split(df, df$a >= 0), function(x) mean(x$b))
#     FALSE     TRUE 
#  33.96727 89.46000 
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
4

The following solutions would do it:

Subset

ndf1<-subset(df, a>=-40 & a<=0)
ndf2<-subset(df, a>=0 & a<=40)

mean(ndf1[,3])
mean(ndf2[,3])

Or simpler

mean(df[a>=-40 & a<=0, 3]) 
mean(df[a>=0 & a<=40, 3]) 

Using ddply

library(plyr)
ddply(df, .(a>=-40 & a<=0), summarize, mean=mean(b))
ddply(df, .(a>=0 & a<=40), summarize, mean=mean(b))
Ruthger Righart
  • 4,799
  • 2
  • 28
  • 33