【问题标题】:R - How to recode multiple columns [duplicate]R - 如何重新编码多列[重复]
【发布时间】:2020-04-06 04:51:10
【问题描述】:

我正在尝试跨多个列将 6s 更改为 NAs。我曾尝试在dplyr 中使用 mutate_at 命令,但似乎无法使其工作。有什么想法吗?

library(dplyr)
ID <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) #Create vector of IDs for ID column.
Score1 <- c(1, 2, 3, 2, 5, 6, 6, 2, 5, 4) #Create vector of scores for Score1 column.
Score2 <- c(2, 2, 3, 6, 5, 6, 6, 2, 3, 4) #Create vector of scores for Score2 column.
Score3 <- c(3, 2, 3, 4, 5, 5, 6, 2, 6, 4) #Create vector of scores for Score3 column.
df <- data.frame(ID, Score1, Score2, Score3) #Combine columns into a data frame.
VectorOfNames <- as.vector(c("Score1", "Score2", "Score3")) #Create a vector of column names.
df <- mutate_at(df, VectorOfNames, 6=NA) #Within the data frame, apply the function (6=NA) to the columns specified in VectorOfNames.

【问题讨论】:

    标签: r dplyr recode


    【解决方案1】:

    dplyr 具有 na_if() 功能,正好可以完成这项任务。你的代码就快到了,可以使用:

    mutate_at(df, VectorOfNames, ~na_if(.x, 6))
    
       ID Score1 Score2 Score3
    1   1      1      2      3
    2   2      2      2      2
    3   3      3      3      3
    4   4      2     NA      4
    5   5      5      5      5
    6   6     NA     NA      5
    7   7     NA     NA     NA
    8   8      2      2      2
    9   9      5      3     NA
    10 10      4      4      4
    

    【讨论】:

      【解决方案2】:

      你可以使用:

      library(dplyr)
      df %>%mutate_at(VectorOfNames, ~replace(., . == 6, NA))
      #OR
      #df %>%mutate_at(VectorOfNames, ~ifelse(. == 6, NA, .))
      
      
      #   ID Score1 Score2 Score3
      #1   1      1      2      3
      #2   2      2      2      2
      #3   3      3      3      3
      #4   4      2     NA      4
      #5   5      5      5      5
      #6   6     NA     NA      5
      #7   7     NA     NA     NA
      #8   8      2      2      2
      #9   9      5      3     NA
      #10 10      4      4      4
      

      或者在基础 R 中:

      df[VectorOfNames][df[VectorOfNames] == 6] <- NA
      

      【讨论】:

        猜你喜欢
        • 2015-10-21
        • 1970-01-01
        • 1970-01-01
        • 2021-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-26
        • 1970-01-01
        相关资源
        最近更新 更多