Reading Date Time information using R

timeseries
r
datetime
date
time_series

#1

Is there anyway to identify the format of Date and Time information (i.e. DD/MM/YYYY HH:MM:SS, AM/PM, EST/IST etc) using R? so that I can use the format with as.Posixct to read the time data properly.
or
Can I convert the date time information to a common format and the read that variable using as.Posixct ?


#2

Hi @Surya1987,

You can simply use as.Date() in R to deal with these type of columns and specify the format that your data is in, explicitly. Like,

dates <- c(“02/27/92”, “02/27/92”, “01/14/92”, “02/28/92”, “02/01/92”)
as.Date(dates,format = “%m/%d/%y”)

Hope this helps.


#3

you can convert the value to as.Date(df$column name) and then you can use as.POSIXct(df$column_name,format = “%m/%d/%Y”, tz = “GMT”)


#4

Hi Saurav,

Sorry for the delayed reply from my side. However this is not going to work in future while reading a csv file which is not in the format you suggested. We can not expect a fixed format of the dates or times while downloading a csv file from any source and trying to read using R.

I hope you understood my concern.