Enric Agud Pique
Enric Agud Pique

Reputation: 1105

Cleaning date string format in R

I would like to clean a date string, it has this form

> print(date)
[1] " 29-12-2014 06:37 UTC"

And I need only 29-12-2014

Upvotes: 1

Views: 2419

Answers (3)

Shubha
Shubha

Reputation: 13

df$newdate <- month(as.POSIXlt(df$Date, format = "%d/%m/%Y"))

df$newdate <- month(as.POSIXlt(df$Date, format = "%d-%m-%Y"))

Upvotes: 0

akrun
akrun

Reputation: 886948

Assuming that you have the strings not in the as.POSIXct format, an option using regex would be

 str1 <- " 29-12-2014 06:37 UTC"
 sub('[ ]+([^ ]+) .*', '\\1', str1)
 #[1] "29-12-2014"

Or using lubridate

library(lubridate)
format(dmy_hm(str1),'%d-%m-%Y')
#[1] "29-12-2014"

This would also take multiple formats

 str2 <- c(str1, '29.12.14 06/37 UTC')
 format(dmy_hm(str2), '%d-%m-%Y')
 #[1] "29-12-2014" "29-12-2014"

Upvotes: 2

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193517

Just use as.Date (assuming your data are already stored as a "date" object).

x <- Sys.time()
x
# [1] "2014-12-29 12:35:18 IST"
as.Date(x)
# [1] "2014-12-29"

If your data are not currently in a standard date format, please use strptime to convert it to a date format first, after which you could also use format

xx <- " 29-12-2014 06:37 UTC"
as.Date(strptime(xx, format = " %d-%m-%Y %H:%M", tz = "UTC"))
# [1] "2014-12-29"

## format would let you specify the order you want
format(strptime(xx, format = " %d-%m-%Y %H:%M", tz = "UTC"), format = "%d-%m-%Y")
# [1] "29-12-2014"

Upvotes: 4

Related Questions