group_by is not working as it expected

Hello,

I have a dataframe look like this

df <-  data.frame(TIME = rep(1:1:24,3),
                  DV = runif(72, min=5, max=99))

i want to get the statistical summary of DV at each time point
i have used the below code, which worked fine earlier , but now it is not grouping but gives the over all result

df2 = df %>%
  group_by(TIME)%>%
  summarise(q5 = quantile(DV,0.05),
            q50 = quantile(DV,0.5),
            q95 = quantile(DV,0.95))
print(df2)
        q5      q50      q95
1 12.27739 53.75661 91.69368

can someone helps me to get summary at each time point .

Thnaks in advance

I think your code works fine for me:

library(tidyverse)

df <-  data.frame(TIME = rep(1:1:24,3),     # why not just rep(1:24, 3)?
                  DV = runif(72, min=5, max=99))

df %>%
  group_by(TIME)%>%
  summarise(q5 = quantile(DV,0.05),
            q50 = quantile(DV,0.5),
            q95 = quantile(DV,0.95))
#> # A tibble: 24 × 4
#>     TIME    q5   q50   q95
#>    <int> <dbl> <dbl> <dbl>
#>  1     1  29.0  73.6  82.1
#>  2     2  68.2  74.0  94.3
#>  3     3  77.6  87.5  91.2
#>  4     4  18.5  77.2  77.9
#>  5     5  14.0  39.3  61.9
#>  6     6  39.2  83.1  91.1
#>  7     7  15.4  95.6  96.6
#>  8     8  29.3  57.3  63.0
#>  9     9  31.3  60.7  61.4
#> 10    10  22.5  60.7  84.7
#> # ℹ 14 more rows

Created on 2023-12-13 with reprex v2.0.2

Change the name of your data.frame. df is a function name and you may have some kind of a conflict. Otherwise I agree with EconProf. Your code is working fine for me.

1 Like

Hi, @EconProf @jrkrideau thanks for your help. I have figured something. Actually the code works fine, but it is problem of loading multiple packages. Code is not working when i load both "dplyr" and "plyr" packages, but it works if i load only "dplyr" package.

Thanks again for your help!

Ah, that sounds reasonable. {dplyr} is descended from {plyr}. There likely are conflicting names.

Did you reverse the order the two packages are loaded? Try it just to satisfy my curiosity!

Sounds likely. I load {data.table} before {tidyverse} in order to not mess up {lubridate}. Humm, maybe I should learn more about {data.table}'s time and date functions.

Oh BTW here is a simple {data.table } equivalent:

library(data.table)
dat1  <-  data.frame(TIME = rep(1:1:24,3), DV = runif(72, min=5, max=99))
DT  <- as.data.table(dat1)

DT[ ,  .(q5 = quantile(DV,0.05),
             q50 = quantile(DV,0.5),
            q95 = quantile(DV,0.95)),
           by = TIME]

There is a package called conflicted that can be useful in these sorts of issues

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.