[R] sorting a data.frame (df) by a vector (which is not contained in the df) - unexpected behaviour of match and factor

drflxms drflxms at googlemail.com
Thu Dec 29 10:44:40 CET 2011


Dear R colleagues,

consider my data.frame named "df" with 3 columns - being level,
prevalence and sensitivity - and 7 rows of data (see dump below).

df <-
structure(list(level = structure(1:7, .Label = c("0", "1", "10",
"100", "1010", "11", "110"), class = "factor"), prevalence =
structure(c(4L,
2L, 3L, 5L, 6L, 1L, 7L), .Label = c("0.488", "0.5", "0.754",
"0.788", "0.803", "0.887", "0.905"), class = "factor"), sensitivity =
structure(c(6L,
1L, 5L, 4L, 3L, 2L, 1L), .Label = c("0", "0.05", "0.091", "0.123",
"0.327", "0.933"), class = "factor")), .Names = c("level", "prevalence",
"sensitivity"), class = "data.frame", row.names = c(NA, -7L))

I'd like to order df by a vector which is NOT contained in the
data.frame. Let's call this vector desiredOrder (see dump below).

desiredOrder <- c("0", "1", "10", "100", "11", "110", "1010")

So after sorting, the order of the level column (df$level) should be in
the order of the vector desiredOrder (as well a the associated data in
the other columns).
I know that this is not an easy task to achieve by order(...) as the
order of desiredOrder isn't a natural one. But I would expect both of
the following to work:

## using match
df[match(df$level,desiredOrder),]

## using factor
df[factor(df$level,levels=desiredOrder),]

Unfortunately the result isn't what I expected: I get a data.frame with
the level column in the order 0,1,10,100,110,1010,11 instead of the
order in desiredOrder (0,1,10,100,11,110,1010).

Does anybody see, what I am doing wrong?
I'd appreciate any kind of help very much!
Best regards, Felix



More information about the R-help mailing list