0

I had a list of numerical values that I wanted to bin using cut(). Now each row has been replaced with the range that it fell into, in the form of ranges using brackets e.g. [0,140] meaning between 0 and 140 inclusive

The problem is these names are lengthy, and eventually require exponent notation, making them even longer, and it makes the graph illegible. Using typeof() it appears it's still in integer form, but I can't figure out how to rename them the way I would with factors. When I tried with factor() and the labels parameter, I was told that sort only worked on atomic lists.

Here's the result of a ggplot2 bar graph. Note the squished labels on the x-axis

As an example, here's essentially what I tried on my dataset, except with the built-in iris dataset:

data(iris)
iris[1] <- cut(iris[[1]], 10, include.lowest=TRUE)
iris[1] <- factor(iris[1], labels = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"))

It returns the error:

Error in sort.list(y) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?
Nicholas Hassan
  • 949
  • 2
  • 10
  • 27
  • 1
    It _is_ a factor. That's what `cut` does: turn a number into a factor. Accordingly, use its `labels` parameter, just like you would with `factor`. – alistaire Mar 02 '17 at 06:57
  • That's strange, I used typeof() and it told me integer. I tried using the labels parameter with factor but it told me it only worked on atomic lists. Do you know how to overcome this? – Nicholas Hassan Mar 02 '17 at 07:00
  • 2
    As will any factor, as they're stored as integers. If you call `class` or `is.factor` to see that it's a factor. Beyond that, you need to make [a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) if you want an answer. – alistaire Mar 02 '17 at 07:02
  • Perhaps I didn't follow the conventions correctly, but could you take a look at the example I've edited into the very bottom of the post? It shows essentially what my steps were except on the iris dataset. – Nicholas Hassan Mar 02 '17 at 07:08
  • 1
    If you subset with one bracket and one dimension, `iris` will keep its structure as a data.frame instead of dropping to a vector. Use `iris[[1]]`, `iris[ , 1]` or `iris$Sepal.Length` instead. – alistaire Mar 02 '17 at 07:12
  • Ah I see now, thanks for explaining – Nicholas Hassan Mar 02 '17 at 07:13

0 Answers0