-1

I have input some data into R which is of two variables. All the data for one of the variables is either 1 or 2 which I am trying to sort into.

When I try data.f<-factor(data) I get the error message

"Error in sort.list(y) : 'x' must be atomic for 'sort.list'

Have you called 'sort' on a list?" which I don't know what this means.

Could anybody suggest how to factor my data?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
r.496
  • 11
  • nobody will be able to help you if you don't provide a reproducible example of your data. take the time to make one, it will be worth it. instructions: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – rmuc8 Apr 03 '15 at 14:10

1 Answers1

2

Following your rather short description and the given error message, I assume that your problem is that you are calling factor on the whole data frame data.

Note that a factor always corresponds to a single variable. As you said you have two variables in your data frame, let's try the following example:

> myDataFrame <- data.frame(x = c(1,2,2,2,1,2,2,1), y = 1:8)
> factor(myDataFrame)
Error in sort.list(y) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?

If you want to compute a factor for the first variable, then do the following instead:

> myFactor <- factor(myDataFrame$x)      # or factor(myDataFrame[,1])

You can then use myFactor to sort your data frame like so:

> myDataFrame[order(myFactor),]
  x y
1 1 1
5 1 5
8 1 8
2 2 2
3 2 3
4 2 4
6 2 6
7 2 7

At last, note that there's actually no need for a factor here. Since your variable is already numerical, you can use it directly as in

> myDataFrame[order(myDataFrame$x),]

which will produce the exact same result.

fotNelton
  • 3,844
  • 2
  • 24
  • 35