Austin Gilbert
Austin Gilbert

Reputation: 43

Conditional Mean Statement Across Dataframe

The following code will return the average conditioned that the months are greater than 6.

mean(df[df$delta1>6, "delta1"], na.rm=T)

Now, how do I do apply this for every column in the dataframe?

df:

delta1      delta2      delta3
NA          2           3
4           NA          6
7           8           NA
10          NA          12
NA          14          15
16          NA          18
19          20          NA

Upvotes: 1

Views: 194

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 389012

We can set the values in the dataframe which are less than equal to 6 to NA and count the mean using colMeans ignoring the NA values.

df[df <= 6] <- NA
colMeans(df, na.rm = TRUE)

#delta1 delta2 delta3 
#    13     14     15 

data

df <- structure(list(delta1 = c(NA, 4L, 7L, 10L, NA, 16L, 19L), delta2 = c(2L, 
NA, 8L, NA, 14L, NA, 20L), delta3 = c(3L, 6L, NA, 12L, 15L, 18L, 
NA)), class = "data.frame", row.names = c(NA, -7L))

Upvotes: 1

DanY
DanY

Reputation: 6073

The apply-family of functions is useful here:

sapply(df, function(x) mean(x[x>6], na.rm=T))

Upvotes: 1

Related Questions