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.