Convert dates and times string to numeric - r

I have a file with nearly four thousand entries in a column formatted like this:
1/28/2015 14:13
How do I get R to read these as real numbers?

As #RomanLuštrik suggested:
mydate <- "1/28/2015 14:13"
# convert to date
strptime(mydate, "%m/%d/%Y %H:%M")
# [1] "2015-01-28 14:13:00 GMT"
# make it numeric
as.numeric(strptime(mydate, "%m/%d/%Y %H:%M"))
# [1] 1422454380

datestring<-"your variable"
x<-strptime(datestring, %b/%d,%Y %H:%M)
Just check out the strptime() info
there is the lubridate package with a lot of functions for this for changing formats
for real numbers you have POSIXct() function.

Related

Formatting String to a Date with Hours and Minutes

I obtained a time string looking like this:
201902041502, containing year, month, day, hour and minute.
Now I want to reformat this string into the german date-time format like this: 04.02.2019 15:02.
I've already tried as.Date and as.POSIXct but it doesnt work and I want to avoid adding seconds to get POSIXct to work.
Thanks in advance! Cheers
You can use strptime to convert the data to a POSIXlt object,
x <- "201902041502"
xd <- strptime(x,"%Y%m%d%H%M")
# [1] "2019-02-04 15:02:00 CET"
and then use strftime to produce your desired format:
strftime(xd, "%d.%m.%Y %H:%M")
# [1] "04.02.2019 15:02"

Convert Factor Dates to Dates in R [duplicate]

This is the information contained within my dataframe:
## minuteofday: factor w/ 89501 levels "2013-06-01 08:07:00",...
## dDdt: num 7.8564 2.318 ...
## minutes: POSIXlt, format: NA NA NA
I need to convert the minute of day column to a date/time format:
minuteave$minutes <- as.POSIXlt(as.character(minuteave$minuteofday), format="%m/%d/%Y %H:%M:%S")
I've tried as.POSIXlt, as.POSIXct and as.Date. None of which worked. Does anyone have ANY thoughts.
The goal is to plot minutes vs. dDdt, but it won't let me plot in the specified time period that I want to as a factor. I have no idea what to try next...
You need to insert an as.character() before parsing as a Datetime or Date.
A factor will always come back first as a number corresponding to its level.
You can save the conversion from factor to character by telling read.csv() etc to no store as a factor: stringsAsFactors=FALSE. You can also set that as a global option.
Once you have it as character, make sure you match the format string to your data:
R> as.POSIXct("2013-06-01 08:07:00", format="%Y-%m-%d %H:%M:%S")
[1] "2013-06-01 08:07:00 CDT"
R>
Note the %Y-%m-%d I used, as opposed to your %m/%d/%y.
Edit on 3 Jan 2016: This is now much easier thanks to the anytime package which automagically converts from many types, including factor, and does so without requiring a format string.
R> as.factor("2013-06-01 08:07:00")
[1] 2013-06-01 08:07:00
Levels: 2013-06-01 08:07:00
R>
R> library(anytime)
R> anytime(as.factor("2013-06-01 08:07:00"))
[1] "2013-06-01 08:07:00 CDT"
R>
R> class(anytime(as.factor("2013-06-01 08:07:00")))
[1] "POSIXct" "POSIXt"
R>
As you can see we just feed the factor variable into anytime() and out comes the desired POSIXct type.
Try this
library(lubridate)
minuteave$minutes <- ymd_hms(minuteave$minutes)
this will return minuteave$minutes as a POSIXct object.
Hope this helps you.

Convert Factor to Date/Time in R

This is the information contained within my dataframe:
## minuteofday: factor w/ 89501 levels "2013-06-01 08:07:00",...
## dDdt: num 7.8564 2.318 ...
## minutes: POSIXlt, format: NA NA NA
I need to convert the minute of day column to a date/time format:
minuteave$minutes <- as.POSIXlt(as.character(minuteave$minuteofday), format="%m/%d/%Y %H:%M:%S")
I've tried as.POSIXlt, as.POSIXct and as.Date. None of which worked. Does anyone have ANY thoughts.
The goal is to plot minutes vs. dDdt, but it won't let me plot in the specified time period that I want to as a factor. I have no idea what to try next...
You need to insert an as.character() before parsing as a Datetime or Date.
A factor will always come back first as a number corresponding to its level.
You can save the conversion from factor to character by telling read.csv() etc to no store as a factor: stringsAsFactors=FALSE. You can also set that as a global option.
Once you have it as character, make sure you match the format string to your data:
R> as.POSIXct("2013-06-01 08:07:00", format="%Y-%m-%d %H:%M:%S")
[1] "2013-06-01 08:07:00 CDT"
R>
Note the %Y-%m-%d I used, as opposed to your %m/%d/%y.
Edit on 3 Jan 2016: This is now much easier thanks to the anytime package which automagically converts from many types, including factor, and does so without requiring a format string.
R> as.factor("2013-06-01 08:07:00")
[1] 2013-06-01 08:07:00
Levels: 2013-06-01 08:07:00
R>
R> library(anytime)
R> anytime(as.factor("2013-06-01 08:07:00"))
[1] "2013-06-01 08:07:00 CDT"
R>
R> class(anytime(as.factor("2013-06-01 08:07:00")))
[1] "POSIXct" "POSIXt"
R>
As you can see we just feed the factor variable into anytime() and out comes the desired POSIXct type.
Try this
library(lubridate)
minuteave$minutes <- ymd_hms(minuteave$minutes)
this will return minuteave$minutes as a POSIXct object.
Hope this helps you.

Converting the data in year month day hour and minutes to date in R

I am trying to convert the date as factor to date using “as.date” function in R. I have the date in the following format
2008-01-01 02:30
I tried to use the following command :
as.Date(mydata$Date, format="%y-%m-%d %h:%mm")
Can somebody help me with this ? I was able to convert the format with no hour but getting difficulty with hour included.
Thank you.
Your format string is incorrect :
R> strptime("2008-01-01 02:30", format="%Y-%m-%d %H:%M")
# [1] "2008-01-01 02:30:00"
See ?strptime for the detailed values you can use to define a format.
Also note that as your string is in a standard format, you can also use directly as.POSIXlt :
R> as.POSIXlt("2008-01-01 02:30")
# [1] "2008-01-01 02:30:00"

Converting between date formats in R?

I have a data.frame (CSV originally) in R with dates are in the following 3 formats:
2011-06-02T17:16:05Z
2012-06-02T17:16:05-07:00
6/2/11 17:16:05
which is year-month-day-time. I don't quite know what the -07:00 is, as it seems to be the same for all timestamps (except for some where it is -08:00), but I guess it's some type of time zone offset.
I am not quite sure what format these are (does anyone know?), but I need to convert it to this format:
6/2/11 17:16:05
which is year-month-day-time
I would like to do this in such a way so that all the dates in the CSV (in one and the same row) is converted to the second format. How can I accomplish this in R?
The full dataset can be found here.
Here's another attempt, assuming your data is text to start with:
test <- c("2011-06-02T17:16:05Z","2012-06-02T17:16:05-07:00")
format(as.POSIXct(test,format="%Y-%m-%dT17:%H:%M"),"%m/%d/%y %H:%M")
[1] "06/02/11 16:05" "06/02/12 16:05"
You can try the following, where myDates would be the column of dates
format(strptime(myDates, format="%Y-%m-%dT17:%H:%M"), format= "%m/%d/%Y %H:%M")
[1] "06/02/2011 16:05" "06/02/2012 16:05"
or with 2-digit year
# Note the lower-case %y at the end
format(strptime(myDates, format="%Y-%m-%dT17:%H:%M"), format= "%m/%d/%y %H:%M")
[1] "06/02/11 16:05" "06/02/12 16:05"
As for the Z, that indicates GMT (think: London).
the -7:00 indicates 7 hours back from GMT (think: Colorado / MST etc)
Please see here for more reference

Resources