Combine column to remove NA's

后端 未结 10 1644
野的像风
野的像风 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:05

    Though this is not the OP case, it seems some people like the approach based on sums, how about thinking in mean and mode, to make the answer more universal. This answer matches the title, which is what many people will find.

    data <- data.frame('a' = c('A','B','C','D','E'),
                       'x' = c(1,2,NA,NA,9),
                       'y' = c(NA,6,3,NA,5),
                       'z' = c(NA,NA,NA,4,5))
    
    splitdf<-split(data[,c(2:4)], seq(nrow(data[,c(2:4)])))
    
    data$mean<-unlist(lapply(splitdf, function(x)  mean(unlist(x), na.rm=T) ) )
    data$mode<-unlist(lapply(splitdf, function(x)  {
      tab <- tabulate(match(x, na.omit(unique(unlist(x) )))); 
                      paste(na.omit(unique(unlist(x) ))[tab == max(tab) ], collapse = ", " )}) )
    
    data
      a  x  y  z     mean mode
    1 A  1 NA NA 1.000000    1
    2 B  2  6 NA 4.000000 2, 6
    3 C NA  3 NA 3.000000    3
    4 D NA NA  4 4.000000    4
    5 E  9  5  5 6.333333    5
    

提交回复
热议问题