【问题标题】:Relabel levels of factors as desired根据需要重新标记因子水平
【发布时间】:2022-11-02 20:09:47
【问题描述】:

我想重新标记因素水平如下:

i. If the level is of length 3 and above relabel it to sentence case otherwise do title case

home doing nothing变成Home doing nothingyes变成Yesgood practice变成Good Practice

有没有办法做到这一点?

library(tidyverse)

vars <- c("a", "b", "c", "d", "e")


mydata <- tribble(
  ~"a", ~"b", ~"c", ~"d", ~"e", ~"id",
  "yes", "school in Kenya", "r", 10, "good practice", 1,
  "no", "home doing nothing", "python", 12, "doing well", 3,
  "no", "school in Tanzania", "c++", 35, "by walking", 4,
  "yes", "home practising", "c", 65, "practising everyday", 5,
  "no", "home", "java", 78, "sitting alone", 7
) %>%
  mutate(across(.cols = vars, ~as_factor(.)))


# mydata %>%
#   mutate(across(where(is.factor), ~fct_relabel(., str_to_sentence(.))))

【问题讨论】:

    标签: r dplyr forcats


    【解决方案1】:

    这是一种可能的解决方案。请注意,您考虑为factor 的列实际上是character 变量,而不是factor

    library(dplyr)
    library(stringr)
    
    mydata %>%
      mutate(across(where(is.character), 
                    ~ if_else(stringi::stri_count_words(.x)>=3, 
                              sub("^([a-z])", "\U\1\E", .x, perl=T), 
                              str_to_title(.x))))
    
    # A tibble: 5 x 6
      a     b                  c          d e                      id
      <chr> <chr>              <chr>  <dbl> <chr>               <dbl>
    1 Yes   School in Kenya    R         10 Good Practice           1
    2 No    Home doing nothing Python    12 Doing Well              3
    3 No    School in Tanzania C++       35 By Walking              4
    4 Yes   Home Practising    C         65 Practising Everyday     5
    5 No    Home               Java      78 Sitting Alone           7
    

    【讨论】:

      【解决方案2】:

      这里有两个选项。 1)您分别评估 >3 和 <3。或者 2) 你同时评估两者。两者在可读性方面各有利弊。

      library(tidyverse)
      
      #option 1
      mydata |>
        mutate(across(where((x) length(levels(x)) >= 3), 
                    (x) fct_relabel(x, str_to_sentence)),
               across(where((x) length(levels(x)) < 3), 
                    (x) fct_relabel(x, str_to_title)))
      #> # A tibble: 5 x 6
      #>   a     b                  c      d     e                   id   
      #>   <fct> <fct>              <fct>  <fct> <fct>               <fct>
      #> 1 Yes   School in kenya    R      10    Good practice       1    
      #> 2 No    Home doing nothing Python 12    Doing well          3    
      #> 3 No    School in tanzania C++    35    By walking          4    
      #> 4 Yes   Home practising    C      65    Practising everyday 5    
      #> 5 No    Home               Java   78    Sitting alone       7
      
      #option 2
      mydata |>
        mutate(across(everything(), (x) if_else(
          rep(length(levels(x)) >= 3, length(x)), 
          fct_relabel(x, str_to_sentence),
          fct_relabel(x, str_to_title)
        )))
      #> # A tibble: 5 x 6
      #>   a     b                  c      d     e                   id   
      #>   <fct> <fct>              <fct>  <fct> <fct>               <fct>
      #> 1 Yes   School in kenya    R      10    Good practice       1    
      #> 2 No    Home doing nothing Python 12    Doing well          3    
      #> 3 No    School in tanzania C++    35    By walking          4    
      #> 4 Yes   Home practising    C      65    Practising everyday 5    
      #> 5 No    Home               Java   78    Sitting alone       7
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-10
        相关资源
        最近更新 更多