【问题标题】:Create new character column in R data frame based on existing character vector根据现有的字符向量在 R 数据框中创建新的字符列
【发布时间】:2020-01-21 14:51:30
【问题描述】:

我是 R 的新手,并且被困在看似简单的任务上 - 在 R 数据框中创建新的列向量,条件是现有的字符向量。

例如,我有一个数据框“类”,其中包含一个字符列(“Names”)和一个数字列(“Student_numbers”):

Names <- c("Sarah", "Mary", "Ben", "Will", "Alex") 
Student_numbers <- c(3,5,6,7,7)
class <- data.frame(Names, Student_numbers) 

对于数据框“类”,我想添加一个名为“性别”的新字符列,它基于字符向量“名称”中的值:

Male <- c("Ben", "Will", "Alex") 
Female <- c("Sarah", "Mary") 

 Names    Student_numbers  Gender
1 Sarah   3                Female
2 Mary    5                Female
3 Ben     6                Male
4 Will    7                Male
5 Alex    7                Male

我不想手动执行此操作,而是希望根据上面定义的字符向量自动执行此操作。

提前感谢您的帮助。

【问题讨论】:

    标签: r


    【解决方案1】:

    此解决方案使用 Tidyverse 库工作:

    library(tidyverse)
    Names <- c("Sarah", "Mary", "Ben", "Will", "Alex") 
    Student_numbers <- c(3,5,6,7,7)
    class <- data.frame(Names, Student_numbers)
    class
    class <- class %>% mutate(gender = ifelse(Names %in% c("Sarah","Mary"),"Female","Male"))
    class
    

    结果是:

      Names    Student_numbers   gender
    1 Sarah               3      Female
    2  Mary               5      Female
    3   Ben               6      Male
    4  Will               7      Male
    5  Alex               7      Male
    

    希望对你有帮助。

    补充:考虑一下您的其他示例,让我们来看看:

    df <- data.frame(dogs = c("Chucho","Pulgas","Pirata","Carcas","Fido","Bigotes"), 
             number_id = c("10","12","15","16","30","19"), stringsAsFactors = FALSE)
    
    df <- df %>% mutate(dog_type = ifelse(dogs %in% c("Chucho","Pulgas"),"Chihuahua",
                               ifelse(dogs %in% c("Pirata","Carcas"),"Hairless Chimu","San Bernardo"))) %>% mutate(dog_size = ifelse(dog_type %in% c("Chihuahua","Hairless Chimu"),"Small","Big"))
    
       dogs      number_id   dog_type          dog_size
    1  Chucho        10      Chihuahua         Small
    2  Pulgas        12      Chihuahua         Small
    3  Pirata        15      Hairless Chimu    Small
    4  Carcas        16      Hairless Chimu    Small
    5  Fido          30      San Bernardo      Big
    6  Bigotes       19      San Bernardo      Big
    

    希望我回答了您的其他问题。

    问候,

    亚历克西斯

    【讨论】:

    • 谢谢亚历克西斯。为了扩展我的问题,如果我有多个类别用于我想要创建的新字符向量。例如。数据框中的现有字符列是关于狗类型(其中有超过 20 个唯一值)的值,我想要一个新的字符列将它们分类为大、中、小或非常小。我会使用“else if”功能吗?提前谢谢你。
    • 你好@Jane Isobel。您可以在示例中使用嵌套的 ifelse 来评估每种狗的类型。理想情况下,您将狗类型和狗大小作为单独数据框中的字典,因此您可以过滤任何其他数据,例如名称和类型的列表。我将在上面的代码中添加示例。
    【解决方案2】:

    你可以在这里使用ifelse

    class$Gender <- ifelse(class$Names %in% Male, 
                           "Male", 
                           ifelse(class$Names %in% Female, "Female", NA))
    class
    #   Names Student_numbers Gender
    # 1 Sarah               3 Female
    # 2  Mary               5 Female
    # 3   Ben               6   Male
    # 4  Will               7   Male
    # 5  Alex               7   Male
    

    如果您有更多案例,您也可以使用来自dplyrcase_when

    library(dplyr)
    case_when(class$Student_numbers < 4 ~ "Grp1",
              class$Student_numbers < 6 ~ "Grp2",
              class$Student_numbers < 7 ~ "Grp3",
              TRUE                      ~ "Other")
    

    【讨论】:

      【解决方案3】:

      你也可以使用sapply和更熟悉的if

      class$gender <- sapply(class$Names, function(x) if(x %in% Male) "Male" else "Female" )
      
       class
       Names Student_numbers gender
      1 Sarah               3 Female
      2  Mary               5 Female
      3   Ben               6   Male
      4  Will               7   Male
      5  Alex               7   Male
      

      我还建议在创建class 时添加stringAsFactors=FALSE,以避免处理factors

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-26
        • 2020-05-04
        • 2019-11-21
        • 1970-01-01
        相关资源
        最近更新 更多