Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format properly date-time column in R using mutate?

Tags:

r

dplyr

I am trying to format a string column to a date-time serie.

The row in the column are like this example: "2019-02-27T19:08:29+000"

(dateTime is the column, the variable)

mutate(df,dateTime=as.Date(dateTime, format = "%Y-%m-%dT%H:%M:%S+0000"))

But the results is:

2019-02-27

What about the hours, minutes and seconds ?

I need it to apply a filter by date-time

like image 899
kamome Avatar asked Feb 27 '19 19:02

kamome


People also ask

How do I format a date column in R?

To format = , provide a character string (in quotes) that represents the current date format using the special “strptime” abbreviations below. For example, if your character dates are currently in the format “DD/MM/YYYY”, like “24/04/1968”, then you would use format = "%d/%m/%Y" to convert the values into dates.

How do I convert a character column to a date column in R?

You can use the as. Date( ) function to convert character data to dates. The format is as. Date(x, "format"), where x is the character data and format gives the appropriate format.


1 Answers

Your code is almost correct. Just the extra 0 and the as.Date command were wrong:

library("dplyr")
df <- data.frame(dateTime = "2019-02-27T19:08:29+000",
           stringsAsFactors = FALSE)

mutate(df, dateTime = as.POSIXct(dateTime, format = "%Y-%m-%dT%H:%M:%S+000"))
like image 181
JBGruber Avatar answered Oct 24 '22 08:10

JBGruber