I want to calculate the quantiles of each row of a data frame and return the result as a matrix. Since I want to calculate and arbitrary number of quantiles (and I imagine that it is faster to calculate them all at once, rather than re-running the function), I tried using a formula I found in this question:
library(dplyr)
df<- as.data.frame(matrix(rbinom(1000,10,0.5),nrow = 2))
interim_res <- df %>%
rowwise() %>%
do(out = sapply(min(df):max(df), function(i) sum(i==.)))
interim_res <- interim_res[[1]] %>% do.call(rbind,.) %>% as.data.frame(.)
This makes sense, but when I try to apply the same framework to the quantile()
function, as coded here,
interim_res <- df %>%
rowwise() %>%
do(out = quantile(.,probs = c(0.1,0.5,0.9)))
interim_res <- interim_res[[1]] %>% do.call(rbind,.) %>% as.data.frame(.)
I get this error message:
Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) :
'x' must be atomic
Why am I getting an error with quantile
and not sum
? How should I fix this issue?