cut_width(labels = FALSE) returns unused argument error

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)

You may have missed this but in the document the labels arguments it document under ... and cut_width does not take any ...

cut_interval(x, n = NULL, length = NULL, ...)

cut_number(x, n = NULL, ...)

cut_width(x, width, center = NULL, boundary = NULL,
  closed = c("right", "left"))

That is why you get unused argument error:

Error in cut_width(variable, width = 1, labels = FALSE) : 
  unused argument (labels = FALSE)

it may be something missing in ggplot2 and you can try opening an issue in the repository for a suggestion of passing ... for passing cut.default argument in cut_width :thinking:

3 Likes

For reference, I opened the question as an issue in ggplot2

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.