Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional Sum in R

Tags:

r

sum

conditional

I have a data frame that is 200 rows by 6 columns. I am interested in computing the total times that a value in Col A is less than a specific number. The number can be hard coded. I do not know where to begin...

like image 801
Sarah Spadanuda Avatar asked May 31 '12 05:05

Sarah Spadanuda


1 Answers

For a slightly more complex problem, use the "which" to tell the "sum" where to sum: if DF is the data frame:

      Ozone Solar.R  Wind Temp Month Day
 1      41     190  7.4   67     5   1
 2      97     267  6.3   92     7   8   
 3      97     272  5.7   92     7   9

Example: sum the values of Solar.R (Column 2) where Column1 or Ozone>30 AND Column 4 or Temp>90

sum(DF[which(DF[,1]>30 & DF[,4]>90),2])
like image 65
Theuns Avatar answered Sep 20 '22 13:09

Theuns