Combine column to remove NA's

后端 未结 10 1629
野的像风
野的像风 2020-11-28 06:44

I have some columns in R and for each row there will only ever be a value in one of them, the rest will be NA\'s. I want to combine these into one column with the non-NA val

相关标签:
10条回答
  • 2020-11-28 07:10

    If you want to stick with base,

    data <- data.frame('a' = c('A','B','C','D','E'),'x' = c(1,2,NA,NA,NA),'y' = c(NA,NA,3,NA,NA),'z' = c(NA,NA,NA,4,5))
    data[is.na(data)]<-","
    data$mycol<-paste0(data$x,data$y,data$z)
    data$mycol <- gsub(',','',data$mycol)
    
    0 讨论(0)
  • 2020-11-28 07:17

    max works too. Also works on strings vectors.

    cbind(data[1], mycol=apply(data[-1], 1, max, na.rm=T))
    
    0 讨论(0)
  • 2020-11-28 07:18

    I would use rowSums() with the na.rm = TRUE argument:

    cbind.data.frame(a=data$a, mycol = rowSums(data[, -1], na.rm = TRUE))
    

    which gives:

    > cbind.data.frame(a=data$a, mycol = rowSums(data[, -1], na.rm = TRUE))
      a mycol
    1 A     1
    2 B     2
    3 C     3
    4 D     4
    5 E     5
    

    You have to call the method directly (cbind.data.frame) as the first argument above is not a data frame.

    0 讨论(0)
  • 2020-11-28 07:20

    One possibility using dplyr and tidyr could be:

    data %>%
     gather(variables, mycol, -1, na.rm = TRUE) %>%
     select(-variables)
    
       a mycol
    1  A     1
    2  B     2
    8  C     3
    14 D     4
    15 E     5
    

    Here it transforms the data from wide to long format, excluding the first column from this operation and removing the NAs.

    0 讨论(0)
提交回复
热议问题