convert value in timedata - r

I´m relative new to R and right now I´m struggling with converting my time data.
I have a list of values, which should be daily data (01/012007-31/12/2015)
str(timedata)
num [1:3103(1d)] 733043 733044 733045 733046 733047 ...
I would like to read the daily dates 01/01/07 02/01/07....
I tried to convert it with the function
as.POSIXct(timedata, origin = "2007-01-01", tz = "GMT")
but the result is wrong:
"2007-01-09 11:39:08 GMT" "2007-01-09 11:39:09 GMT" "2007-01-09 11:39:10 GMT"
"2007-01-09 11:39:11 GMT" "2007-01-09 11:39:12 GMT"...
maybe someone could help me?! I guess it should be possible to get my dates, but which function makes sense?

This gives a daily vector of dates from Jan 1, 2007 to Dec 31, 2015:
tsD <- seq.Date("2007-01-01", "2015-12-31")
You could also coerce that vector to Dates. But to do so you need to use the correct origin for your data which appears to be the erroneous starting date chosen by the Lotus 1-2-3 developers and perpetuated by Microsoft:
tsD <- as.Date(my.timedata, origin="0000-01-01")
I actually don't know how to do that is one step. So I would be subtracting 1 from those values to get a corrected date series. Read ?as.Date
> tsD
[1] "2007-01-02" "2007-01-03" "2007-01-04" "2007-01-05" "2007-01-06"
If you want the dates to be displayed as 'dd/mm/yyyy' you need to read about formatting strings in ?strptime but use the generic format function which has a Date method:
> format(tsD, format="%d-%m-%Y")
[1] "02-01-2007" "03-01-2007" "04-01-2007" "05-01-2007" "06-01-2007"

Related

How to convert matlab numeric to POSIXct [duplicate]

I have some MATLAB serial date number that I need to use in R but I havt to convert them to a normal date.
Matlab:
datestr(733038.6)
ans =
27-Dec-2006 14:24:00
you can see it gives the date and time.
Now we try in R:
Matlab2Rdate <- function(val) as.Date(val - 1, origin = '0000-01-01')
> Matlab2Rdate(733038.6)
[1] "2006-12-27"
It gives only the date but I need also the time? Any idea
The trick is Matlab uses "January 01, 0000", a fictional reference date, to calculate its date number. The origin of time for the "POSIXct" class in R is, ‘1970-01-01 00:00.00 UTC’. You can read about how different systems handle dates here.
Before converting, you need to account for this difference in reference from one format to another. The POSIX manual contains such an example. Here's my output:
> val<-733038.6
> as.POSIXct((val - 719529)*86400, origin = "1970-01-01", tz = "UTC")
[1] "2006-12-27 14:23:59 UTC"
Where 719529 is ‘1970-01-01 00:00.00 UTC’ in Matlab's datenum and 86400 the number of seconds in an standard UTC day.

Converting timestamp in seconds to a date format in R

I have a table (tags) with a column for timestamp (ts), which is formatted as seconds since 1 Jan, 1970 GMT. I'm trying to create a date column that converts the timestamp from seconds to date and time EST.
The suggested code for R was:
tags$date<-strptime(tags$ts, "%Y-%m-%d")
tags$date<-as.POSIXct(tags$date)
But when I do this, tags$date comes up as NA. Any suggestions for what I might be doing wrong? Thanks.
You should us as.POSIXct function instead:
tags$date <- as.POSIXct(tags$ts, origin="1970-01-01", tz="US/New York")
strptime converts between character representations and dates not between timestamp and dates.
Here's a lubridate version. When we use as_datetime we don't need to explicitly specify an origin as it defaults to the desired origin.
lubridate::as_datetime(1507119276, tz='EST')
# [1] "2017-10-04 07:14:36 EST"

How can I keep timezone shifts when converting characters to POSIXct

I have a large dataframe with a column containing date-times, encoded as a factor variable.My Sys.timezone() is "Europe/Berlin". The date-times have this format:
2015-05-05 17:27:04+05:00
where +05:00 represents the timeshift from GMT. Importantly, I have multiple timezones in my dataset, so I cannot set a specific timezone and ignore the last 6 characters of the strings. This is what I tried so far:
# Test Date
test <- "2015-05-05 17:27:04+05:00"
# Removing the ":" to make it readable by %z
A <- paste(substr(test,1,22),substr(test,24,25),sep = "");A
# Returns
# "2015-05-05 17:27:04+0500"
output <- as.POSIXct(as.character(A, "%Y-%B-%D %H:%M:%S%z"))
# Returns
# "2015-05-05 17:27:04 CEST"
The output of "CEST" for +0500 is incorrect. Moreover, when I run this code on the whole column I see that every date is coded as CEST, regardless of the offset.
How can I keep the specified timezone when converting to POSIXct?
In order to facilitate the process you can use lubridate package.
E.g.
library("lubridate")#load the package
ymd_hms("2015-05-05 17:27:04+05:00",tz="GMT")#set the date format
[1] "2015-05-05 12:27:04 GMT"
Therefore you keep the timezone info. Finally:
as.POSIXct(ymd_hms("2015-05-05 17:27:04+05:00",tz="GMT"),tz = "GMT")#transform the date into another timezone
[1] "2015-05-05 12:27:04 GMT"

Convert character to date vector (!bY) in R [duplicate]

This question already has an answer here:
How to convert a character string date to date class if day value is missing
(1 answer)
Closed 5 years ago.
I am trying to convert a character vector of dates (in the format: i.e. "Jan.1990") to a date vector (keeping a similar format: i.e. "Jan 1990" or "Jan.1990").
month_year <- ("Jan.1990", "Feb.1990", "Mar.1990", "Jan.1991", "Feb.1991", Mar. 1991")
I have tried using as.Date and various commands with the lubridate package but I either end up with an incorrect format or NAs.
I tried using as.Date:
df$month_year <- as.Date(df$month_year,format = "%b%Y")
This resulted in NAs.
I tried lubridate::parse_date_time2
parse_date_time2(tidy_labor$month_year, c("Jan.1990"), exact = TRUE, orders = "bY")
But the format came out as:
unknown timezone 'Jan.1990'unknown timezone 'Jan.1990' [1] "1990-01-01 GMT" "1990-01-01 GMT" "1990-01-01 GMT" "1990-01-01 GMT" "1990-02-01 GMT" "1990-02-01 GMT" "1990-02-01 GMT"
Any help would be much appreciated.
This question comes up a lot and the short answer is that dates require a day.
You could use zoo::as.yearmon:
library(zoo)
as.yearmon("Jan.1990", "%b.%Y")
Or you could assume that the day is always the first of the month, concatenate that to your values and convert to Date type.

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