Thanks to visit codestin.com
Credit goes to github.com

Skip to content

overwriting ordered factors using fct_recode fails #42

@elbersb

Description

@elbersb
library(tidyverse)
d = tibble(x = ordered(c("apple", "bear", "banana", "dear"))) 
# works
fct_recode(d$x, fruit = "apple", fruit = "banana")
#> [1] fruit bear  fruit dear 
#> Levels: fruit < bear < dear

# works when using a new variable
tidylog::mutate(d, y = fct_recode(x, fruit = "apple", fruit = "banana"))
#> mutate: new variable 'y' with 3 unique values and 0% NA
#> # A tibble: 4 x 2
#>   x      y    
#>   <ord>  <ord>
#> 1 apple  fruit
#> 2 bear   bear 
#> 3 banana fruit
#> 4 dear   dear

# fails when overwriting
tidylog::mutate(d, x = fct_recode(x, fruit = "apple", fruit = "banana"))
#> Error in Ops.factor(new, old): level sets of factors are different

# works with factor
d = tibble(x = factor(c("apple", "bear", "banana", "dear"))) 
tidylog::mutate(d, x = fct_recode(x, fruit = "apple", fruit = "banana"))
#> mutate: changed 2 values (50%) of 'x' (0 new NA)
#> # A tibble: 4 x 1
#>   x    
#>   <fct>
#> 1 fruit
#> 2 bear 
#> 3 fruit
#> 4 dear

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions