0

What is a way to count the number of digits of a numeric object in R including leading zeroes?

For example, I know nchar(x) will return the number of digits if x is numeric but what about instances in which x includes leading zeros?

Note: Count the number of integer digits does not address the issue of leading zeros.

Example:

x<-7
nchar(7)
[1] 1 #that's fine

x<-07
nchar(07)
[1] 1 #that is NOT fine: I want the value of 2 to appear
Cyrus Mohammadian
  • 4,982
  • 6
  • 33
  • 62
  • 5
    I think you are asking the impossible. Even though you use the assignment `x <- 07`, the numeric internal representation of x is just 7. Of course, you can convert x to a character string containing leading zeros, and then count the number of characters in the string. – ericOss Apr 17 '19 at 23:25
  • @ericOss yeah I figured as much, I thought I'd ask though. Converting to string first is what im currently doing but that route presents other problems for me. Thanks for confirming my suspicion nonetheless. Feel free to post the comment as a response and I'll accept. – Cyrus Mohammadian Apr 17 '19 at 23:35

0 Answers0