0

I'm trying to perform a simple summarize operation using a trimmed mean by referencing a trim-value column. I keep getting length errors and I cannot understand why this is not working. Maybe I'm missing something obvious?

I don't need any special aggregation, just grouping by one column. All the reference values are the same for each group. I've also tried grouping by both columns (the group and reference columns), but still get the same error.

library(dplyr)

d <- data.frame(
           grp = c("a", "a", "a", "b", "b", "b"),
           val = seq(1, 16, 3),
           ref = c(0.1, 0.1, 0.1, 0.2, 0.2, 0.2)
)

d %>%
  group_by(grp) %>%
  summarize(m = mean(val, trim = ref))

Error: Evaluation error: 'trim' must be numeric of length one.

qab
  • 1
  • 1
  • 2
    The error message states that the trim argument expects a single value but in this case it's being passed multiple (identical) values. In your example you can use `mean(val, trim = unique(ref))`. – Ritchie Sacramento Jun 12 '19 at 23:10
  • Yes! This did the trick, thank you! – qab Jun 13 '19 at 02:27

0 Answers0