R: coalescing a large data frame
问题 Say I create a data frame, foo : foo <- data.frame(A=rep(NA,10),B=rep(NA,10)) foo$A[1:3] <- "A" foo$B[6:10] <- "B" which looks like, A B 1 A <NA> 2 A <NA> 3 A <NA> 4 <NA> <NA> 5 <NA> <NA> 6 <NA> B 7 <NA> B 8 <NA> B 9 <NA> B 10 <NA> B I can coalesce this into a single column, like this: data.frame(AB = coalesce(foo$A, foo$B)) giving, AB 1 A 2 A 3 A 4 <NA> 5 <NA> 6 B 7 B 8 B 9 B 10 B which is nice. Now, say my data frame is huge with lots of columns. How do I coalesce that without naming each