mercredi 27 décembre 2017

Sum by date using "if" statement

I am trying to sum between two dates in a data frame using an "if" statement.

date = seq(as.Date("2000-01-01"), as.Date("2000-01-31"), by="days")
nums = seq(1, 1, length.out = 31)
df = data.frame(date, nums)

if(df$date >= as.Date("2000-01-01") && df$date <= as.Date("2000-01-07")){
  sum(df$nums)
}

However, the output is "31" rather than "7" as I would expect. Is there a better way to sum by date? I would like to use an "if" statement because I would like to apply this to a much larger dataset with many different columns and within different lengths of time.

Aucun commentaire:

Enregistrer un commentaire