I have a set of daily time series data for several years in R -
i have set of daily time series data several years (20-70 years). dates defined dd/mm/yyyy in 1 column, , daily flow values in another. intend sort , extract maximum flow each year in r.
try 1 of aggregating functions.
aggregate(flow~cbind(year=substr(year,7,11)), df1, fun=max) # year flow #1 2001 23 #2 2002 26
or
library(data.table) setdt(df1)[, list(flow= max(flow)) ,.(year=substr(year, 7, 11))] # year flow #1: 2001 23 #2: 2002 26
another option converting 'date' class , extract 'year' part.
library(lubridate) setdt(df1)[, list(flow=max(flow)), .(year= year(dmy(year)))]
data
set.seed(24) df1 <- data.frame(year= c('26/05/2001', '27/05/2001', '02/01/2002', '03/01/2002'), flow= sample(20:30,4, replace=false), stringsasfactors=false)
Comments
Post a Comment