Taliman
Taliman

Reputation: 57

Something wrong with sorting

I have a data frame (df) like this

Name   Value
P       9.1
M       14.3
P       12.4
C       10.5

I tried sorting it using

df[order(df$value),]

and get something like this

Name      Value
C          10.5
P          12.4 
M          14.3 
P           9.1

Can you tell me what is going on?

Upvotes: 1

Views: 58

Answers (1)

IRTFM
IRTFM

Reputation: 263352

Probably it's a factor. If it were a character column it would get printed with quotation marks, so this is the fix to the ordering:

df[ order(as.numeric(as.character(df$value))), ]

Upvotes: 1

Related Questions