Search code examples
ras.datenchar

Number of characters in an object of class Date in R


I am trying to figure out the rationale for the number of characters in an R object of class Date, which seems to be different than I would expect based on manually counting the length of a date string. For example:

str1 <- "2020-12-11"
dat1 <- as.Date(str1)
nchar(str1); nchar(dat1)

Why is the character length of str1 10 characters (the expected result), while the length of dat1 is only 5? Is there any way around this that results in dat1 being 10 characters?


Solution

  • It is converting to storage integer mode

    as.integer(dat1)
    #[1] 18607
    
    mode(dat1)
    #[1] "numeric"
    
    nchar(dat1)
    #[1] 5
    

    where the number of characters is 5

    while as a string, it is also counting the characters -