-3

I have a dataframe containing values. I want to take an average of each column but I want to only take into consideration those values that are greater or equal to 300.

Example: Dataframe[,1]
      100
      200
      250
      300
      300
      400
      400

    Average = 350.
Cricketer
  • 399
  • 1
  • 3
  • 20

1 Answers1

0
mean(ifelse(Dataframe[,1] >= 300, Dataframe[,1], NA), na.rm = TRUE)

and for all columns at once:

do.call("cbind", lapply(DataFrame, function(x){mean(ifelse(x >= 300, x, NA), na.rm = TRUE)}))
easports611
  • 442
  • 3
  • 12
  • Just use standard subsetting `mean(Dataframe[,1][Dataframe[,1] >= 300])` or `lapply(Dataframe, function(x) mean(x[x >= 300]) )` – thelatemail Jun 04 '15 at 05:20