[R] Extending each element in a list, or rbind()-ing arrays of different length without recycling

Stavros Macrakis macrakis at alum.mit.edu
Fri Feb 13 18:49:08 CET 2009

Combining the various approaches on the list, here's a simple
one-liner that puts the NAs at the end:

     t(apply(mat,1,function(r) { dr<-duplicated(r); c( r[!dr],
rep(NA,sum(dr)) ) ))

If you don't care where the NAs are, the following is a tad shorter
and perhaps clearer:

     mat[ t(apply(mat,1,duplicated)) ] < -NA                  # modifies mat


More information about the R-help mailing list