0

I'm trying to create a df of quantiles from a long df. The code I'm using is the following:

ex_long <- ex %>% pivot_longer(cols = starts_with("layer"),values_to = "val") %>% group_by(ID)

> str(ex_long)
grouped_df [3,116,100 x 3] (S3: grouped_df/tbl_df/tbl/data.frame)
 $ ID  : num [1:3116100] 1 1 1 1 1 1 1 1 1 1 ...
 $ name: chr [1:3116100] "layer.1" "layer.2" "layer.3" "layer.4" ...
 $ val : num [1:3116100] 16.6 28.4 20.4 19 19.9 ...
 - attr(*, "groups")= tibble [86 x 2] (S3: tbl_df/tbl/data.frame)
  ..$ ID   : num [1:86] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ .rows: list<int> [1:86] 

qnt = c(.25, .75, .5, .025, .975)
tab <- ex_long %>% group_by(ID) %>% summarize(q25 = quantile(val, probs = qnt[1]), q75 = quantile(val, probs = qnt[2]),
                             Mediana = quantile(val, probs = qnt[3]), q2.5 = quantile(val, probs = qnt[4]), 
                             q97.5 = quantile(val, probs = qnt[5]))

> tab
       q25      q75  Mediana     q2.5    q97.5
1 11.75951 20.08258 15.91438 3.921109 27.99679

My problem is that this code returns a df with one row with the calculations done without the grouping. Can someone explain why am I wrong?

  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Nov 06 '21 at 20:26
  • 1
    At the very least show teh output of `str(ex_long)` – IRTFM Nov 06 '21 at 20:32
  • Sorry, just edited to add the str of the df – noob_researcher Nov 06 '21 at 21:04

1 Answers1

0

changed summarize() by dplyr::summarize() and it worked