5

I am trying to round off my labels from cut function in R using dig.lab argument. I have given value as 20 but I get lot of decimal places after number in labels e.g. (114126.30000000001746,5248999] . And if I reduce the value of dig.lab to 5, the labels come in scientific notation for e.g. (1.1413e+05,5.249e+06]

I want to round it off and limit to 4 decimal places with no scientific notation, any suggestions?

rapunzel
  • 53
  • 1
  • 5
  • When you `cut` it seems to change the values to factors. What I would do is change these back to numeric and from there I would use the `round` function with 4 decimal places. – Hansel Palencia Dec 03 '19 at 18:33
  • Do you really need the decimal places (4 or other)? Would integer break points do? – Rui Barradas Dec 03 '19 at 18:53
  • @RuiBarradas I thought about that but I need decimal places. – rapunzel Dec 03 '19 at 19:01
  • @HanselPalencia I am sorry I dont understand what you mean. The output of cut function is in label format like (114126.30000000001746,5248999] , how do I format these? – rapunzel Dec 03 '19 at 19:45
  • Not sure, you could try regex. cut all non-integer characters, and then remove anything after +4 past the period. – Hansel Palencia Dec 03 '19 at 20:11
  • See `cut(runif(5), breaks=c(-1,0.5,1))` versus `cut(runif(5), breaks=c(-1,0.5,1), labels=c("aa","bb"))`. – r2evans Dec 03 '19 at 20:20
  • While it's likely not that difficult (with a well-formed regex), I recommend *against* trying to reformat labels assigned by `cut`, I would think it would be better to resolve this before calling `cut`. – r2evans Dec 03 '19 at 20:26

1 Answers1

1

Though I believe that r2evans is right, here it goes. Note that in the test example I have left the output of cut untouched.

newLabels <- function(x, dig.lab = 4){
  lev <- levels(x)
  pattern <- paste0("^[\\(\\[][-]*\\d*\\.\\d{",
                    dig.lab,
                    "}|,[-]*\\d*\\.\\d{",
                    dig.lab,
                    "}"
  )
  m <- gregexpr(pattern = pattern, levels(x))
  y <- regmatches(lev, m)
  y <- sapply(y, paste, collapse = "")
  y <- paste0(y, substring(lev, nchar(lev)))
  y
}

set.seed(1234)
x <- runif(1000, 0, 6e6)
y <- cut(x, breaks = 10, dig.lab = 20)
z <- factor(y, labels = newLabels(y, dig.lab = 4))

levels(z)
#[1] "(-3942.8915,601427.6033]"    "(601427.6033,1200804.3310]" 
#[3] "(1200804.3310,1800181.0587]" "(1800181.0587,2399557.7864]"
#[5] "(2399557.7864,2998934.5141]" "(2998934.5141,3598311.2418]"
#[7] "(3598311.2418,4197687.9694]" "(4197687.9694,4797064.6971]"
#[9] "(4797064.6971,5396441.4248]" "(5396441.4248,6001811.9198]"
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66