I'm not sure I understand, but it looks like you want to move NA to the end of the columns? Here is one way (quick, maybe cleaner):
> d <- data.frame(rbind(c(1, 2, NA, 4, NA, 6), c(NA, 2, 3, 4, 5, 6))) > d X1 X2 X3 X4 X5 X6 1 1 2 NA 4 NA 6 2 NA 2 3 4 5 6 > t(apply(d, 1, function(x) c(x[!is.na(x)], rep(NA, sum(is.na(x)))))) [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 2 4 6 NA NA [2,] 2 3 4 5 6 NA
According to your data:
> t(apply(data, 1, function(x) c(x[!is.na(x)], rep(NA, sum(is.na(x)))))) [,1] [,2] [,3] [,4] [1,] "20/02/01" "16/04/01" NA NA [2,] "23/03/02" "03/05/02" "12/02/03" NA
Vince source share