1

I have a data frame x with columns a,b,c.

I am trying to order the data based on columns a and c and my ordering criteria is ascending on a and descending on c. Below is the code I wrote.

z <- x[order(x$a,-x$c),]

This code gives me a warning as below.

Warning message:
In Ops.factor(x$c) : - not meaningful for factors

Also, when I check the data frame z using head(z), it gives me wrong data as below:

30708908 0.3918980    NA
22061768 0.4022183    NA
21430343 0.4118651    NA
21429828 0.4134888    NA
21425966 0.4159323    NA
22057521 0.4173094    NA

and initially there wasnt any NA values of the column c in the data frame x. I have gone through a lot of threads but couldn't find any solution. Can anybody please suggest.

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
Kunal Batra
  • 1,001
  • 3
  • 15
  • 23
  • 1
    One of your columns is a factor. Try `str(x)`. You can convert to numeric (assuming nothing funky is going on) via `as.factor(as.character(x))`. I also suggest reading http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Roman Luštrik Feb 13 '13 at 07:54
  • @RomanLuštrik: Do you mean `as.numeric(as.character(x))` rather than `as.factor`? – David Robinson Feb 13 '13 at 07:58
  • Mea culpa, yes I mean `numeric`. Time to go back to bed. :) – Roman Luštrik Feb 13 '13 at 08:00

2 Answers2

1

try this

install.packages('plyr');
library('plyr');
z<-arrange(x,a,desc(c));

In addition, you can use the

options(stringsAsFactors = FALSE) 

before you create your frame, or while creating your 'x' data frame, specify

stringsAsFactors = FALSE
Aditya Sihag
  • 5,057
  • 4
  • 32
  • 43
  • @Roman Luštrik: I tried using the above mentioned method after using stringsAsFactors=FALSE option. It again gave me NA with the below warning message: top<-x[order(x$a,-as.numeric(as.character(x$c)) ), ] In order(x$a, -as.numeric(as.character(x$c))) : NAs introduced by coercion head(top) a b c 30708908 0.3918980 NA 22061768 0.4022183 NA 21430343 0.4118651 NA 21429828 0.4134888 NA 21425966 0.4159323 NA 22057521 0.4173094 NA – Kunal Batra Feb 13 '13 at 09:19
  • Using arrange also gave me NAs. library("plyr"); z<-arrange(x,a,desc(c)) > head(z) a b c 1 0.3918980 NA 2 0.4022183 NA 3 0.4118651 NA 4 0.4134888 NA 5 0.4159323 NA 6 0.4173094 NA – Kunal Batra Feb 13 '13 at 09:20
  • @DWin: I tried using the above mentioned method after using stringsAsFactors=FALSE option. It again gave me NA with the below warning message: top<-x[order(x$a,-as.numeric(as.character(x$c)) ), ] In order(x$a, -as.numeric(as.character(x$c))) : NAs introduced by coercion head(top) a b c 30708908 0.3918980 NA 22061768 0.4022183 NA 21430343 0.4118651 NA 21429828 0.4134888 NA 21425966 0.4159323 NA 22057521 0.4173094 NA – Kunal Batra Feb 13 '13 at 10:11
1
z <- x[order(x$a,-as.character(x$c) ), ]
z

If as Roman suspects you have digits in your facttor levels you may need to do as he suggests and add as.numeric, otherwise 9 will be greater than 10

z <- x[order(x$a,-as.numeric(as.character(x$c)) ), ]
z

But if they are characters, then you will again get all NAs, so it really depends on the nature of the levels of x$c

IRTFM
  • 258,963
  • 21
  • 364
  • 487