0

I am attempting to add a leading zero to all observations of a column that have 4 or fewer digits.

var1 <- data.frame(1000, 2000, 3000, 4000)

My current attempt has been to estimate the number of digits and if it exceeds 4, not to paste 0 on it. If it does have 4 or under digits, then it gets a zero at the front.

lead <- function(x, y) {
  nDigits <- function(x) nchar(trunc(abs(y$x)))
  if(nDigits <= 4)
    paste0(x, 0)
}

When I ran this code, I received an error message stating "Error in nDigits <= 4 : comparison (4) is possible only for atomic and list types." Is there an easier way to run this and/or estimate the number of values within an observation?

hpr
  • 21
  • 1

0 Answers0