1

I have Data as

    Date1       Date2         Date3
    2005-12-22  NA            NA
    2009-10-11  NA            NA
    NA          2005-04-11    NA
    NA          NA            2008-11-06
    NA          NA            2006-01-02
    NA          2005-04-16    2006-01-02

I want to collapse elements with NA and get like:

Date1       Date2         Date3
2005-12-22  2005-04-11     2008-11-06       
2009-10-11  2005-04-16     2006-01-02      
                           2006-01-02
Kara
  • 6,115
  • 16
  • 50
  • 57
VASISTA
  • 75
  • 5

1 Answers1

4

If we don't mind losing the order, then maybe try this:

apply(df1, 2, sort, na.last = TRUE)

To keep the order:

sapply(1:ncol(df1),
       function(i){
         c(
           df1[, i][!is.na(df1[, i])],
           df1[, i][ is.na(df1[, i])]
           )
         })
zx8754
  • 52,746
  • 12
  • 114
  • 209