Here is my problem:
I use a function that returns a named vector. Here is an example of a toy:
toy_fn <- function(x) { y <- c(mean(x), sum(x), median(x), sd(x)) names(y) <- c("Right", "Wrong", "Unanswered", "Invalid") y }
I use group_by in dplyr to apply this function to each group (typical split-apply-comb). So here is my data.frame toy:
set.seed(1234567) toy_df <- data.frame(id = 1:1000, group = sample(letters, 1000, replace = TRUE), value = runif(1000))
And here is the result I was aiming for:
toy_summary <- toy_df %>% group_by(group) %>% summarize(Right = toy_fn(value)["Right"], Wrong = toy_fn(value)["Wrong"], Unanswered = toy_fn(value)["Unanswered"], Invalid = toy_fn(value)["Invalid"]) > toy_summary Source: local data frame [26 x 5] group Right Wrong Unanswered Invalid 1 a 0.5038394 20.15358 0.5905526 0.2846468 2 b 0.5048040 15.64892 0.5163702 0.2994544 3 c 0.5029442 21.62660 0.5072733 0.2465612 4 d 0.5124601 14.86134 0.5382463 0.2681955 5 e 0.4649483 17.66804 0.4426197 0.3075080 6 f 0.5622644 12.36982 0.6330269 0.2850609 7 g 0.4675324 14.96104 0.4692404 0.2746589
It works! But itβs just not great to call the same function four times. I would prefer dplyr to get a named vector and create a new variable for each element of the vector. Something like that:
toy_summary <- toy_df %>% group_by(group) %>% summarize(toy_fn(value))
This, unfortunately, does not work, because "Error: one value expected."
I thought, well, just translate the vector into data.frame using data.frame(as.list(x)) . But that doesn't work either. I tried many things, but I couldnβt trick dplyr into thinking that it actually gets one value (observation) for 4 different variables. Is there any way to help dplyr understand this?