1

How to rid of default scientific notation for factor variable? It is used by R when I have 3 or more digits in the current case.

I looked through other answers (scipen and other, format), but it seems they do not work for factor variables.

I use then this factor variable in the legend, and I need integer or fixed point values.

options(scipen=999)
plot(0,0, type="l", lty=1, lwd=4);

var1 <- c(3334,341,36,341,456)
cut1 <- cut(var1, breaks=2)
cut1<-levels(cut1)
cut1<-gsub(","," - ",cut1)
cut1<-gsub("\\(","[",cut1)
#cut1 <- as.character(cut1);
#formatC(cut1, digits="10", format="f")
cut1

[1] " [32.7 - 1.68e+03]" "[1.68e+03 - 3.34e+03]

And I want something like that:

[1] " [32.7 - 1680]" "[1680 - 3340]"

Community
  • 1
  • 1
John_West
  • 2,239
  • 4
  • 24
  • 44
  • The issue is nothing to do with the variable being a factor. It's purely due to the `cut()` function, and the fact that it ignores `options('scipen'), options('digits')` etc. – smci Mar 26 '17 at 02:26

1 Answers1

4

The point where you have to specify what levels you are interested in, is a cut function. So easiest way is:

cut1 <- cut(var1, breaks=2, dig.lab = 5)
[1] (1685,3337.3] (32.702,1685] (32.702,1685] (32.702,1685] (32.702,1685]
Levels: (32.702,1685] (1685,3337.3]

See ?cut for more information.

bartoszukm
  • 693
  • 3
  • 10