[R] Sorting with NA skipped (not omitted)
Dieter Menne
dieter.menne at menne-biomed.de
Mon Feb 1 09:13:34 CET 2010
As a C-programmer, doing complex sorting based on a callback function is
fairly easy. After having read some messages on the forum, using a callback
function for sorting is not that easy in R.
Assume I want that in a "<" comparison, NA should be treated as "don't
decide, leave decision to the next level". In a callback function, I would
treat an NA in one of the terms as equal to the second.
How to do this in R? I am aware that this definition leads to transitivity
problems, but the result is good enough for the qualitative display I am
looking for.
The real job could have been solved easily with imputation, but the
customers believes this is voodoo, so I have to find a way around it.
Dieter
df = data.frame(x=c(1,1,1),y=c(1,NA,3),z=c(1,2,3))
df[order(df$x,df$y,df$z,na.last=FALSE),] # NA row is last
df[order(df$x,df$y,df$z,na.last=TRUE),] # NA row is first
# Note that na.last = NA is definitively wrong
# NA row should be second
More information about the R-help
mailing list