fct_relevel not working, change level order but not actual data

78 views Asked by At

I'm trying to manually order a variable and running into issues. I tried using fct_relevel in sevearl different ways, but that doesn't actually change the order of the variable "organism" in the dataset. When I check using levels the order is correct, but the actual data hasn't changed.

I also tried just factor, but that didn't work either.

I feel like it's an easy fix, but can't seem to figure it out. Would appreciate any help!

Here's the reprex:

pacman::p_load(tidyverse)

dat <- data.frame(
    stringsAsFactors = FALSE,
    record_id = c(281L,94L,2L,
                  56L,133L,390L,272L,121L,279L,343L,314L,
                  324L,116L,225L,378L,72L,37L,254L,103L,249L,
                  319L,384L,47L,135L,39L,212L,35L,252L,1L,
                  112L,391L,88L,171L,283L,105L,131L,311L,
                  53L,150L,203L,220L,312L,4L,164L,385L,354L,
                  13L,266L,308L,111L),
    esbl_markers = c("CTX-M-27",
                     "CTX-M-15","CTX-M-15","VEB-6","CTX-M-15",
                     "CTX-M-15","CTX-M-27","CTX-M-27","CTX-M-15",
                     "CTX-M-15","CTX-M-15","CTX-M-15","CTX-M-15",
                     "CTX-M-15","CTX-M-15","VEB-6","CTX-M-15","CTX-M-15",
                     "CTX-M-15","CTX-M-15","CTX-M-15","SHV-2",
                     "CTX-M-15","CTX-M-15","CTX-M-3","CTX-M-27",
                     "CTX-M-15","CTX-M-15, OXY-1-1","CTX-M-15",
                     "SHV-187, CTX-M-15","CTX-M-15","CTX-M-15","CTX-M-15",
                     "SHV-187","CTX-M-15","CTX-M-15","CTX-M-15",
                     "CTX-M-15","CTX-M-15","SHV-7","CTX-M-15",
                     "CTX-M-15","CTX-M-27","CTX-M-15","CTX-M-15","CTX-M-3",
                     "CTX-M-15","CTX-M-15","CTX-M-15","CTX-M-15"),
    organism = as.factor(c("Klebsiella pneumoniae",
                           "Escherichia coli","Escherichia coli",
                           "Escherichia coli","Escherichia coli","Other",
                           "Klebsiella oxytoca",
                           "Escherichia coli","Klebsiella pneumoniae",
                           "Klebsiella pneumoniae","Klebsiella pneumoniae",
                           "Klebsiella pneumoniae",
                           "Escherichia coli","Escherichia coli","Other",
                           "Escherichia coli","Escherichia coli",
                           "Escherichia coli","Escherichia coli",
                           "Escherichia coli",
                           "Klebsiella pneumoniae","Other","Escherichia coli",
                           "Escherichia coli","Escherichia coli",
                           "Escherichia coli","Escherichia coli",
                           "Escherichia coli","Escherichia coli",
                           "Escherichia coli","Other",
                           "Escherichia coli","Escherichia coli",
                           "Klebsiella pneumoniae","Escherichia coli",
                           "Escherichia coli","Klebsiella pneumoniae",
                           "Escherichia coli",
                           "Escherichia coli","Escherichia coli",
                           "Escherichia coli","Klebsiella pneumoniae",
                           "Escherichia coli","Escherichia coli","Other",
                           "Klebsiella pneumoniae",
                           "Escherichia coli","Klebsiella oxytoca",
                           "Klebsiella pneumoniae","Escherichia coli"))
)

clean_dat <- dat |> 
    mutate(organism = fct_relevel(organism, c("Escherichia coli", "Klebsiella pneumoniae", "Klebsiella oxytoca", "Other")))

clean_dat <- dat |>
    ungroup() |> 
    mutate(organism = fct_relevel(organism, "Klebsiella oxytoca", after = 2))

levels(clean_dat$organism)
#> [1] "Escherichia coli"      "Klebsiella pneumoniae" "Klebsiella oxytoca"   
#> [4] "Other"
head(clean_dat)
#>   record_id esbl_markers              organism
#> 1       281     CTX-M-27 Klebsiella pneumoniae
#> 2        94     CTX-M-15      Escherichia coli
#> 3         2     CTX-M-15      Escherichia coli
#> 4        56        VEB-6      Escherichia coli
#> 5       133     CTX-M-15      Escherichia coli
#> 6       390     CTX-M-15                 Other

clean_dat <- dat |> 
    mutate(organism = factor(organism, levels = c("Escherichia coli", "Klebsiella pneumoniae", "Klebsiella oxytoca", "Other")))

Created on 2023-12-18 with reprex v2.0.2

Session info
sessionInfo()
#> R version 4.2.2 (2022-10-31 ucrt)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19045)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=English_United States.utf8 
#> [2] LC_CTYPE=English_United States.utf8   
#> [3] LC_MONETARY=English_United States.utf8
#> [4] LC_NUMERIC=C                          
#> [5] LC_TIME=English_United States.utf8    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#>  [1] lubridate_1.9.2 forcats_1.0.0   stringr_1.5.0   dplyr_1.1.0    
#>  [5] purrr_1.0.1     readr_2.1.4     tidyr_1.3.0     tibble_3.2.0   
#>  [9] ggplot2_3.4.1   tidyverse_2.0.0
#> 
#> loaded via a namespace (and not attached):
#>  [1] compiler_4.2.2   pillar_1.8.1     tools_4.2.2      digest_0.6.31   
#>  [5] timechange_0.2.0 evaluate_0.20    lifecycle_1.0.3  gtable_0.3.1    
#>  [9] pkgconfig_2.0.3  rlang_1.0.6      reprex_2.0.2     cli_3.6.0       
#> [13] rstudioapi_0.14  yaml_2.3.7       xfun_0.37        fastmap_1.1.1   
#> [17] withr_2.5.0      knitr_1.42       generics_0.1.3   fs_1.6.1        
#> [21] vctrs_0.5.2      hms_1.1.2        grid_4.2.2       tidyselect_1.2.0
#> [25] glue_1.6.2       R6_2.5.1         fansi_1.0.4      rmarkdown_2.20  
#> [29] pacman_0.5.1     tzdb_0.3.0       magrittr_2.0.3   ellipsis_0.3.2  
#> [33] scales_1.2.1     htmltools_0.5.4  colorspace_2.1-0 utf8_1.2.3      
#> [37] stringi_1.7.12   munsell_0.5.0
1

There are 1 answers

0
Ali On

Possibly you do not need to see the data in order, but you can do it with clean_dat |> arrange(organism) or clean_dat |> arrange(desc(organism)).