I'm trying to use cut_width
within mutate
in order to bin my data, which does work. However, when I try passing labels = FALSE
in cut_width
I get an error. But according to the documentation (https://ggplot2.tidyverse.org/reference/cut_interval.html), it should convert the labels into integers, or am I misinterpreting things?
Here's a brief reprex:
library(tidyverse)
data <- tibble::tribble(
~variable, ~value,
1, 5L,
1, 10L,
1, 15L,
2, 10L,
3, 13L,
3, 14L,
5, 80L,
1.5, 20L,
6, 50L,
6, 50L
)
# Find mean per bin (works as expected)
data %>%
mutate(binned_variable = cut_width(variable, width = 1)) %>%
group_by(binned_variable) %>%
summarise(mean_val = mean(value))
#> # A tibble: 5 x 2
#> binned_variable mean_val
#> <fct> <dbl>
#> 1 [0.5,1.5] 12.5
#> 2 (1.5,2.5] 10
#> 3 (2.5,3.5] 13.5
#> 4 (4.5,5.5] 80
#> 5 (5.5,6.5] 50
# Expecting binned_variable to be 1,2,3,4,5, but get error:
data %>%
mutate(binned_variable = cut_width(variable, width = 1, labels = FALSE)) %>%
group_by(binned_variable) %>%
summarise(mean_val = mean(value))
#> Error in mutate_impl(.data, dots): Evaluation error: unused argument (labels = FALSE).
Created on 2019-01-01 by the reprex package (v0.2.1)