Elzo Valugi Elzo Valugi - 11 months ago 58
R Question

Integer to string conversion in R

I am trying to find a function to transform an integer to a string so I can use it in initiate a Date object.

> v = c(20081101)
> date <- as.Date(v, format("%Y%m%d"))

Error in charToDate(x) : character string is not in a standard
unambiguous format

I found a solution with
, but I would like to know what is the correct way to do the conversion.

> date <- as.Date(paste(v), format("%Y%m%d"))
> date
[1] "2008-11-01"

Answer Source

as.character() would be the general way rather than use paste() for its side effect

> v <- 20081101
> date <- as.Date(as.character(v), format = "%Y%m%d")
> date
[1] "2008-11-01"

(I presume this is a simple example and something like this:

v <- "20081101"

isn't possible?)