-1

I am trying to sort a data frame by a column of numbers and I get an alphanumeric sorting of the digits instead. If the data frame is converted to a matrix, the sorting works.

df[order(as.numeric(df[,2])),]

   V1 V2
1  a  1
3  c 10
2  b  2
4  d  3

> m <- as.matrix(df)
> m[order(as.numeric(m[,2])),]

      V1  V2  
[1,] "a" "1" 
[2,] "b" "2" 
[3,] "d" "3" 
[4,] "c" "10"
scs
  • 567
  • 6
  • 22

3 Answers3

2
V1 <- letters[1:4]
V2 <- as.character(c(1,10,2,3))
df <- data.frame(V1,V2, stringsAsFactors=FALSE)
df[order(as.numeric(df[,2])),]

gives

  V1 V2
1  a  1
3  c  2
4  d  3
2  b 10

But

V1 <- letters[1:4]
V2 <- as.character(c(1,10,2,3))
df <- data.frame(V1,V2)
df[order(as.numeric(df[,2])),]

gives

  V1 V2
1  a  1
2  b 10
3  c  2
4  d  3

which is due to factors.

thanks to the commentators akrun and Imo. Inspect each of the two dfs with str(df).

Also, there is more detail given the factor() function help menu. Scroll down to 'Warning' for more details of the issue at hand.

DarrenRhodes
  • 1,431
  • 2
  • 15
  • 29
1

Could you be a little more specific about what's your intial dataframe ? Because by running this code :

df<-data.frame(c("a","b","c","d"),c(1,2,10,3))
colnames(df)<-c("V1","V2")

#print(df)

df.order<-df[order(as.numeric(df[,2])),]

print(df.order)

I get the right answer :

  V1 V2
1  a  1
2  b  2
4  d  3
3  c 10
IvannMG
  • 41
  • 4
-2

Edit:

The column values might be being treated as factors.

Try forcing to character and then integer.

Example copy and pasted from console:

> Foo <- data.frame('ABC' = c('a','b','c','d'),'123' = c('1','2','10','3'))
> Foo[order(as.integer(as.character(Foo[,2]))),]
  ABC X123
1   a    1
2   b    2
4   d    3
3   c   10
ChristyCasey
  • 426
  • 5
  • 9