这是我的玩具数据集:
library(tidyverse)
dat <- tibble (x1 = c("False - very long label specific to x1", "False - very long label specific to x1", "True - very long label specific to x1", "True - very long label specific to x1"),
x2 = c("False - very long label specific to x2", "False - very long label specific to x2", "False - very long label specific to x2", "True - very long label specific to x2"),
y = c(10, 5, 12, 4)) %>% mutate_at(vars(x1:x2), factor)
head(dat)
#> # A tibble: 4 x 3
#> x1 x2 y
#> <fct> <fct> <dbl>
#> 1 False - very long label specific~ False - very long label specific~ 10
#> 2 False - very long label specific~ False - very long label specific~ 5
#> 3 True - very long label specific ~ False - very long label specific~ 12
#> 4 True - very long label specific ~ True - very long label specific ~ 4
我想修剪很长的因子标签,它们都有两个共同点:
- 都以True或False开头
- 包括列名(即每列的因子标签是唯一的)
我想简化这一点,并且每个因子列只有True和False 之类的东西。这是我想要的输出:
#> # A tibble: 4 x 3
#> x1 x2 y
#> <fct> <fct> <dbl>
#> 1 False False 10
#> 2 False False 5
#> 3 True False 12
#> 4 True True 4
我认为它应该与类似mutate_at
andfct_relabel
和可能的东西一起使用str_trunc
,但我无法弄清楚。