How to combine two columns of a data-frame with missing data? [duplicate]

时光怂恿深爱的人放手 提交于 2019-12-11 10:32:58

问题


This is an extension of this earlier question. How can I combine two columns of a data frame as

data <- data.frame('a' = c('A','B','C','D','E'),
                   'x' = c("t",2,NA,NA,NA),
                   'y' = c(NA,NA,NA,4,"r"))

displayed as

'a' 'x' 'y'  
 A   t   NA  
 B   2   NA  
 C  NA   NA  
 D  NA   4  
 E  NA   r

to get

 'a' 'mycol'  
  A   t  
  B   2  
  C   NA  
  D   4  
  E   r  

I tried this

cbind(data[1], mycol = na.omit(unlist(data[-1])))

But it obviously doesn't keep the NA row.


回答1:


You could do it by using ifelse, like this:

data$mycol <- ifelse(!is.na(data$x), data$x, data$y)

> data

##   a  x  y mycol
## 1 A  1 NA     1
## 2 B  2 NA     2
## 3 C NA NA    NA
## 4 D NA  4     4
## 5 E NA  5     5



回答2:


Going with your logic, you can do following:

cbind(data[1], mycol = unlist(apply(data[2:3], 1, function(i) ifelse(
  length(is.na(i))==length(i),
  na.omit(i),
  NA)
)))

#  a mycol
#1 A     1
#2 B     2
#3 C    NA
#4 D     4
#5 E     5



回答3:


This has been addressed here indirectly. Here is a simple solution based on that:

data$mycol <- coalesce(data$x, data$y)



回答4:


Extending the answer to any number of columns, and using the neat max.col() function I've discovered thanks to this question:

coalesce <- function(value_matrix) {
  value_matrix <- as.matrix(value_matrix)
  first_non_missing <- max.col(!is.na(value_matrix), ties.method = "first")
  indices <- cbind(
    row = seq_len(nrow(value_matrix)),
    col = first_non_missing
  )
  value_matrix[indices]
}

data$mycol <- coalesce(data[, c('x', 'y')])
data
#   a  x  y mycol
# 1 A  1 NA     1
# 2 B  2 NA     2
# 3 C NA NA    NA
# 4 D NA  4     4
# 5 E NA  5     5

max.col(..., ties.method = "first") returns, for each row, the index of the first column with the maximum value. Since we're using it on a logical matrix, the max is usually TRUE. So we'll get the first non-NA value for each row. If the entire row is NA, then we'll get an NA value as desired.

After that, the function uses a matrix of row-column indices to subset the values.

Edit

In comparison to mrip's coalesce, my max.col is slower when there are a few long columns, but faster when there are many short columns.

coalesce_reduce <- function(...) {
  Reduce(function(x, y) {
    i <- which(is.na(x))
    x[i] <- y[i]
    x},
    list(...))
}

coalesce_maxcol <- function(...) {
  value_matrix <- cbind(...)
  first_non_missing <- max.col(!is.na(value_matrix), ties.method = "first")
  indices <- cbind(
    row = seq_len(nrow(value_matrix)),
    col = first_non_missing
  )
  value_matrix[indices]
}

set.seed(100)

wide <- replicate(
  1000,
  {sample(c(NA, 1:10), 10, replace = TRUE)},
  simplify = FALSE
)

long <- replicate(
  10,
  {sample(c(NA, 1:10), 1000, replace = TRUE)},
  simplify = FALSE
)

microbenchmark(
  do.call(coalesce_reduce, wide),
  do.call(coalesce_maxcol, wide),
  do.call(coalesce_reduce, long),
  do.call(coalesce_maxcol, long)
)
# Unit: microseconds
#                           expr      min        lq       mean   median       uq      max neval
# do.call(coalesce_reduce, wide) 1879.460 1953.5695 2136.09954 2007.303 2152.654 5284.583   100
# do.call(coalesce_maxcol, wide)  403.604  423.5280  490.40797  433.641  456.583 2543.580   100
# do.call(coalesce_reduce, long)   36.829   41.5085   45.75875   43.471   46.942   79.393   100
# do.call(coalesce_maxcol, long)   80.903   88.1475  175.79337   92.374  101.581 3438.329   100


来源:https://stackoverflow.com/questions/45824666/how-to-combine-two-columns-of-a-data-frame-with-missing-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!