convert parse string to date in R

How to convert string to date in R

Here is how to convert a string to a date in R. A string might contain a date for various reasons. Those reasons change the complexity of the solution. For example, you might have a date as a string after extracting date from a date-time or merging separate date components. Sometimes it might happen by copying data from Excel to R.

 

Convert string to date in R

If a date in a string in the default R date format is YYYY-MM-DD, you can convert it into the date by using base functionality.
The easiest way is to use function as.Date.

stringdate <- c("2022-07-01", "2022-07-02", "2022-07-03")

class(stringdate)

#[1] "character"



as.Date(stringdate)

#[1] "2022-07-01" "2022-07-02" "2022-07-03"

There are various ways to convert string to date by using lubridate functions. In this case, it looks like this.

require(lubridate)

ymd(stringdate)

#[1] "2022-07-01" "2022-07-02" "2022-07-03"

 

Parse region-specific string dates in R

If you are copying your data from Excel to R, there might be a date format that is not like YYYY-MM-DD.

In that situation, you should additionally define a format. That helps R to read that correctly. Be aware that there is a difference between capital Y and y in the format specification.

Here is an example of how to parse the U.S. date format.

USdate <- c("07/01/2022", "07/02/2022", "07/03/2022")


as.Date(USdate, format = "%m/%d/%Y") 

#[1] "2022-07-01" "2022-07-02" "2022-07-03"



require(lubridate)

mdy(USdate)

#[1] "2022-07-01" "2022-07-02" "2022-07-03"

Here is an example of how to parse the European date format.

EUdate1 <- c("01/07/2022", "02/07/2022", "03/07/2022")


as.Date(EUdate1, format = "%d/%m/%Y") 

#[1] "2022-07-01" "2022-07-02" "2022-07-03"

dmy(EUdate1)

#[1] "2022-07-01" "2022-07-02" "2022-07-03"

There might be variations, but as so far, specify the format or use the appropriate lubridate function.

EUdate2 <- c("01.07.2022", "02.07.2022", "03.07.2022")


as.Date(EUdate2, format = "%d.%m.%Y") 

#[1] "2022-07-01" "2022-07-02" "2022-07-03"

dmy(EUdate2)

#[1] "2022-07-01" "2022-07-02" "2022-07-03"

 

Convert string with two-digit year to date in R

If you have a two-digit year in the string, then be aware then there is a difference between capital Y and y in the format. If you know the difference, it is no problem to get the correct results.

notfullyear <- c("07/01/22", "07/02/22", "07/03/22")


as.Date(notfullyear, format = "%m/%d/%y") 

#[1] "2022-07-01" "2022-07-02" "2022-07-03"



require(lubridate)

mdy(notfullyear)

#[1] "2022-07-01" "2022-07-02" "2022-07-03"

 

Convert string to Posix in R

In the previous examples result after parsing dates in R was in the date class.

stringdate <- c("2022-07-01", "2022-07-02", "2022-07-03")

class(as.Date(stringdate))

#[1] "Date"

If you want to parse string dates into Posix, here is a result with a specified timezone.

as.POSIXct(stringdate, tz = "UTC")

#[1] "2022-07-01 UTC" "2022-07-02 UTC" "2022-07-03 UTC"

Different timezones might cause problems in calculations or merging data.

It is also possible to specify the format.

EUdate1 <- c("01/07/2022", "02/07/2022", "03/07/2022")


as.POSIXct(EUdate1, format = "%d/%m/%Y", tz = "UTC")

#[1] "2022-07-01 UTC" "2022-07-02 UTC" "2022-07-03 UTC"





Posted

in

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *