I have a question about calculating percentiles for each year in several years. How to do this? A small sample is below, but I have more than 10 years of daily data.
DF = data.frame(
year=c(1980, 1980, 1980, 1980, 1981, 1981, 1981, 1981),
month =c(12, 12, 12, 12, 1, 1, 1, 1),
day= c(28, 29, 30, 31, 1, 2, 3, 4),
value=c(0.60, 0.21, 0.43, 0.44, 0.24, 0.29, 0.21, 0.29))
I want to calculate the 20th percentile of the values in each year. How to do this? I just know the sum, mean, max, and min in aggregate() function. For example, the sum of values in each year is calculated as
value.yr = aggregate(value~year, sum, data=DF)
The 20th percentile in year 1980 is:
value.1980= quantile(subset(DF, year==1980)$value, 0.2)
But if I want to calculate the 20th percentile of the values in each year and put them in a vector, how to adapt the code to something like this?
value.20th = aggregate(value~year, FUN = quantile(0.2), data=DF)
Thanks for any help.