0

I have a situation such like this:

df<-data.frame(A=c(1, NA), B=c(NA, 2), C=c(3, NA), D=c(4, NA), E=c(NA, 5))
df
   A  B  C  D  E
1  1 NA  3  4 NA
2 NA  2 NA NA  5

What I wanted is, conditioning on all length(!is.na(df$*))==1, reduce df to :

df
   A  B  C  D  E
 1 1  2  3  4  5
David Z
  • 6,641
  • 11
  • 50
  • 101
  • 2
    Is `colSums(df, na.rm = TRUE)` useful? It doesn't do the NA check you mention, but not sure if that's a absolute must-have or not based on the sample data you provided. – Sam Jun 29 '16 at 18:47

1 Answers1

3

As long as the resulting rows are equal, you can use:

dfNew <- do.call(data.frame, lapply(df, function(i) i[!is.na(i)]))

which results in

dfNew
A B C D E
1 1 2 3 4 5
lmo
  • 37,904
  • 9
  • 56
  • 69