【问题标题】:Combining/uniting/merging two columns in ONE data set ignoring NAs [duplicate]合并/合并/合并一个数据集中的两列,忽略 NA [重复]
【发布时间】:2019-12-10 11:28:49
【问题描述】:

我是 R 新手,需要数据清理方面的帮助。

在我的数据集(称为“调查”)中,我想将两列合并/合并/合并(但是)两列:“性别”和“Geschlecht”列应该是一列,称为“性别”。

我使用了以下命令: Survey$Sex <- paste(Survey$Gender, "", Survey$Geschlecht)

这就是我的结果:

  Gender   Geschlecht        Sex 
1   NA          1           NA  1
2   NA          1           NA  1
3   NA          1           NA  1
4   NA          0           NA  0
5   NA          0           NA  0
6   NA          0           NA  0

我想删除/省略“性别”列中的 NA

像这样(期望的结果):

  Gender   Geschlecht      Sex 
1   NA          1           1
2   NA          1           1
3   NA          1           1
4   NA          0           0
5   NA          0           0
6   NA          0           0

我该怎么做? :-) 请帮忙!

【问题讨论】:

    标签: r paste na data-cleaning


    【解决方案1】:

    您还可以使用dplyrcoalesce() 函数。以 GKi 的回答为例:

    library(dplyr)
    
    Survey <- data.frame(Gender = c(NA,NA,0,1,0), Geschlecht = c(0,1,1,NA,NA))
    
    Survey %>%
        mutate(Sex = coalesce(Gender, Geschlecht))
    

    【讨论】:

      【解决方案2】:

      您可以使用ifelseGeschlechtGender 列之间进行选择。

      Survey <- data.frame(Gender = c(NA,NA,0,1,0), Geschlecht = c(0,1,1,NA,NA))
      Survey$Sex <- ifelse(is.na(Survey$Gender), Survey$Geschlecht, Survey$Gender)
      Survey
      #  Gender Geschlecht Sex
      #1     NA          0   0
      #2     NA          1   1
      #3      0          1   0
      #4      1         NA   1
      #5      0         NA   0
      

      【讨论】:

      • 您好 GKi,感谢您的回答。它按照您的建议在 R 中工作,但现在当我想查看我的数据集“调查”时,只会出现您的答案中显示的可能事件。如何将它应用于我的真实数据集?
      • @Lena 重新读取您的数据 Survey 并执行 Survey$Sex &lt;- ifelse(is.na(Survey$Gender), Survey$Geschlecht, Survey$Gender)
      • 抱歉再次询问,我有点困惑:在您建议的命令到我的数据集之后,我该如何返回到那时应该全部应用它的数据集?
      • @Lena 您是如何获得名为Survey 的数据集的?
      • 我就是这样命名的
      【解决方案3】:

      基础 R 解决方案:

      # 1. Keeping only the "Sex" Vector: 
      
      Survey_clean <- within(Survey, 
      
                            {
      
                             Sex <- rowSums(replace(Survey, is.na(Survey), 0));
      
                             rm(Gender, Geschlecht)
      
                              }
      
                             )
      
      # 2. Keeping all vectors: 
      
      Survey$Sex <- rowSums(replace(Survey, is.na(Survey), 0))
      

      Tidyverse 解决方案:

      # Install pacakges if they are not already installed: 
      
      necessary_packages <- c("dplyr")
      
      # Create a vector containing the names of any packages needing installation: 
      
      new_packages <- necessary_packages[!(necessary_packages %in% installed.packages()[,"Package"])]
      
      # If the vector has more than 0 values, install the new pacakges
      # (and their) associated dependencies: 
      
      if(length(new_packages) > 0){
      
        install.packages(new_packages, dependencies = TRUE)
      
      }
      
      # Initialise the packages in the session: 
      
      lapply(necessary_packages, require, character.only = TRUE)
      
      
      #1. Keeping only the sex vector as the others are now redundant: 
      
      Survey %>%
        transmute(Sex = coalesce(Gender, Geschlecht))
      
      #2. Keeping all vectors:
      
      Survey %>% 
        mutate(Sex = coalesce(Gender, Geschlecht))
      

      数据谢谢@GKi:

      Survey <- data.frame(Gender = c(NA,NA,0,1,0), Geschlecht = c(0,1,1,NA,NA))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-21
        • 1970-01-01
        • 1970-01-01
        • 2019-01-21
        • 1970-01-01
        • 2022-11-11
        • 2017-08-05
        • 2013-03-01
        相关资源
        最近更新 更多