Reputation: 153
Suppose I have following time-series data frame in R.
Date Var1
1/1/2010 7
1/2/2010 154
1/3/2010 125
1/4/2010 87
1/5/2010 57
1/6/2010 12
....
....
7/24/2015 5
If I want Sum(Var1) on year 2010 only, how to do it in R?
Upvotes: 2
Views: 1929
Reputation: 887078
We can use substr
to get the 'year' part from 'Date' column, use that in subset
to extract the rows that have '2010' as year, select the 'Var1' column, and get the sum
sum(subset(df1, substr(Date,5,8)==2010, select=Var1))
Or a dplyr/lubridate
option would be using filter
and summarise
to get similar result.
library(lubridate)
library(dplyr)
df1 %>%
filter(Date=year(mdy(Date))==2010) %>%
summarise(Var1= sum(Var1))
Upvotes: 3