Error with percentile

Hello everybody,

I have data like that. I would like to calculate the breakpoints at 30% and 70% for variable "BTM" each year.
I tried this code but this code has an error. I would like to ask for help. Thank you in advance.

df<-data.frame(
        yyyy = c(2000L,2000L,2001L,2001L,2001L,2001L,
                 2001L,2001L,2002L,2002L),
         BTM = c(4.99137e+21,3.352767396e+21,
                 5.26505365887288e+21,2.09113916730247e+21,4.23970980946175e+20,
                 8.061477105e+21,3.378213072e+21,1.52745289670368e+20,
                 2.89211394115133e+21,1.00022139200115e+21),
      ticker = as.factor(c("REE","SAM","BBC",
                           "CAN","LAF","REE","SAM","SGH","BBC","BPC")),
        code = as.factor(c("REE2000","SAM2000",
                           "BBC2001","CAN2001","LAF2001","REE2001","SAM2001",
                           "SGH2001","BBC2002","BPC2002"))

My code

percentile<- df%>% group_by(yyyy) %>% 
  quantile(BTM,probs = c(0.3, 0.7),na.rm=TRUE)

Best regards,

The quantile function will not be affected by the result of the group_by function and it also does not know that BTM is a column within df. You need to wrap quantile within summarize() to fix those two problems and then have quantile return one value at a time.

percentile<- df%>% group_by(yyyy) %>% 
  summarize(Q30 = quantile(BTM,probs = c(0.3),na.rm=TRUE),
         Q70 = quantile(BTM,probs = c(0.7),na.rm=TRUE))
1 Like

Thank you for for quick reply.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.