I wish to extract the top n largest values from time series data e.g. For Jan, display top n values; For Feb, display top 10 values, etc.
#Data set example
df <- data.frame(
variables = rep(c("height", "weight", "mass", "IQ", "EQ"), times = 12),
month = rep(1:12, each = 5),
values = rnorm(60, 3, 1)
)
head(df, 10)
variables month values
1 height 1 1.859971
2 weight 1 3.985432
3 mass 1 4.755852
4 IQ 1 1.507079
5 EQ 1 2.816110
6 height 2 2.394953
7 weight 2 3.256810
8 mass 2 3.776439
9 IQ 2 3.038668
10 EQ 2 3.540750
Trying to extract top 3 values each month but I'm getting this error:
df %>%
group_by(month) %>%
summarise(top.three = top_n(3))
Error in UseMethod("tbl_vars") :
no applicable method for 'tbl_vars' applied to an object of class "c('double', 'numeric')"
Could anyone advise please? Thanks.