0

I have several columns with non-overlapping data in it:

a <- c(rep(1, 10), rep(NA, 20))
b <- c(rep(NA, 10), rep(2, 10), rep(NA, 10))
c <- c(rep(NA, 20), rep(3, 10))
data <- cbind(a, b, c)

an output:

       a  b  c
 [1,]  1 NA NA
 [2,]  1 NA NA
 [3,]  1 NA NA
 [4,]  1 NA NA
 [5,]  1 NA NA
 [6,]  1 NA NA
 [7,]  1 NA NA
 [8,]  1 NA NA
 [9,]  1 NA NA
[10,]  1 NA NA
[11,] NA  2 NA
[12,] NA  2 NA
[13,] NA  2 NA
[14,] NA  2 NA
[15,] NA  2 NA
[16,] NA  2 NA
[17,] NA  2 NA
[18,] NA  2 NA
[19,] NA  2 NA
[20,] NA  2 NA
[21,] NA NA  3
[22,] NA NA  3
[23,] NA NA  3
[24,] NA NA  3
[25,] NA NA  3
[26,] NA NA  3
[27,] NA NA  3
[28,] NA NA  3
[29,] NA NA  3
[30,] NA NA  3

What is the way to collapse these N columns (there are much more than 3) into one using dplyr, so

result <- c(rep(1, 10), rep(2, 10), rep(3, 10))

Of course in the real data instead of (1, 2, 3) the actual data is something completely different and its only common property is that it is not NA.

Philipp Chapkovski
  • 1,949
  • 3
  • 22
  • 43

1 Answers1

3

In base R,

  1. For numeric, you could use rowSums
    rowSums(data, na.rm = TRUE)
  1. For non-numeric, max.col can be used to identify the columns with non NA values
   data[cbind(1:NROW(data), max.col(!is.na(data)))]
d.b
  • 32,245
  • 6
  • 36
  • 77