将一个变量中的级别名称添加到另一个变量中的级别名称

时间:2018-06-20 21:20:18

标签: r dataframe dplyr

是否有一种简单的方法将一个变量中的级别名称添加到另一个变量中的级别名称?

为说明起见,请考虑以下数据框:

DF1  <- data.frame(Gender = factor(c("Male", "Female", "Female", "Female", "Male", "Male")),
                   Wealth=factor(c("Rich", "Poor", "Rich", "Poor", "Rich", "Poor")),
                   Education=factor(c("College", "College", "HS", "College", "HS", "HS")))

我想将Gender变量中的性别添加到其他两个变量中的级别名称中,例如:

DF2 <- data.frame(Gender = factor(c("Male", "Female", "Female", "Female", "Male", "Male")),
                 Wealth=factor(c("MALE: Rich", "FEMALE: Poor", "FEMALE: Rich", "FEMALE: Poor", "MALE: Rich", "MALE: Poor")),
                 Education=factor(c("MALE: College", "FEMALE: College", "FEMALE: HS", "FEMALE: College", "MALE: HS", "MALE: HS")))

怎么办?

1 个答案:

答案 0 :(得分:2)

这是一种tidyverse方法,可轻松指定要应用于选择列的函数。请注意,在许多情况下,因素都适合进行字符串操作。在这里,我们用str_to_upper改变大小写,用str_c连接列,然后转换回因数。

DF1 <- data.frame(
  Gender = factor(c("Male", "Female", "Female", "Female", "Male", "Male")),
  Wealth = factor(c("Rich", "Poor", "Rich", "Poor", "Rich", "Poor")),
  Education = factor(c("College", "College", "HS", "College", "HS", "HS"))
)

library(tidyverse)
DF2 <- DF1 %>%
  mutate_at(
    vars(Wealth, Education),
    funs(factor(str_c(str_to_upper(Gender), ": ", .)))
    )
DF2 %>% as_tibble()
#> # A tibble: 6 x 3
#>   Gender Wealth       Education      
#>   <fct>  <fct>        <fct>          
#> 1 Male   MALE: Rich   MALE: College  
#> 2 Female FEMALE: Poor FEMALE: College
#> 3 Female FEMALE: Rich FEMALE: HS     
#> 4 Female FEMALE: Poor FEMALE: College
#> 5 Male   MALE: Rich   MALE: HS       
#> 6 Male   MALE: Poor   MALE: HS

reprex package(v0.2.0)于2018-06-20创建。