0

I would like to calculate quantiles for each combination of two categorical variables. E.g.

df <- data.frame(x=1:100, y=c(rep("A", 50), rep("B", 50)), y2=c(rep("C", 25), rep("D", 25),rep("E", 25), rep("F", 25)))

I tried solution shown in Use ntile() with group_by() with dplyr by extending to group_by(y,y2) %>% mutate(z=ntile(x,2)) but did not get the expected result of two quantile groups per combination of y and y2.

df1 <- df %>%
  group_by(y) %>% mutate(z = ntile(x, 2))
df2 <- df %>% 
  group_by(y,y2) %>% mutate(z = ntile(x, 2))
df1 == df2 #suggests they are identical
gdeniz
  • 169
  • 9
  • 1
    Have you gone through all rows of `df1 == df2`? Are you sure they are all `TRUE` in the `z` column? You can use `identical(df1, df2)` to compare data frames – benson23 May 18 '22 at 02:39
  • Yes I checked again. `identical(data.frame(df1),data.frame(df2))` is `TRUE`. – gdeniz May 18 '22 at 02:44
  • 1
    You need to run your code again. Just run exactly what you posted and have different dataframes – Onyambu May 18 '22 at 02:47
  • You are correct. Just ran it on a another computer and they are different. Must be a conflict with a package? Any ideas? – gdeniz May 18 '22 at 02:53
  • 1
    restart your R session – Onyambu May 18 '22 at 03:00

0 Answers0