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

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.

Related

Convert Numeric Value to DateTime [duplicate]

This question already has answers here:
Lubridate as_date and. as_datetime differences in behavior
(2 answers)
Closed 1 year ago.
I have the below numeric value. I need to convert it into datetime format.
1627074000000
To see more values like this (for e.g. 1627077600000), you can refer the link below -
https://coindatadesktop.com/coins/getRelatives.php?symbol=VNDC&limit=48&modo=pre
I googled a bit and found (here) it's in GMT format.
<startTimeGmtMs>1627243200000</startTimeGmtMs>
<endTimeGmtMs>1627248600000</endTimeGmtMs>
I tried this but returns invalid date -
as.POSIXct(1627243200000, origin = "1970-01-01", tz = "GMT")
This seems to be in miliseconds:
try:
as.POSIXct(1627243200000/1000, origin = "1970-01-01", tz = "GMT")
[1] "2021-07-25 20:00:00 GMT"

Date in R - converting to custom layout from given format [duplicate]

This question already has answers here:
R - Help in Converting factor to date (%m/%d/%Y %H:%M)
(4 answers)
Closed 4 years ago.
I have a date value "11/7/2016 11:51" which is currently in "mm/dd/yyyy hh:mm" format. I want to convert this date to "2016-11-07 11:51:00" i.e "yyyy-mm-dd hh:mm:ss" format using R language.
I would like to have any suggestions/help. Thanks in advance.. !!
Or mdy_hm from lubridate
lubridate::mdy_hm("11/7/2016 11:51")
[1] "2016-11-07 11:51:00 UTC"
lubridate will default to timezone UTC.
as.POSIXct will default to your (computer) timezone.
We can use as.POSIXct from base R
as.POSIXct( "11/7/2016 11:51" , format = "%m/%d/%Y %H:%M")
#[1] "2016-11-07 11:51:00 IST"
If we want to change the timezone, there is tz argument

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 value in timedata

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"

Convert dates and times string to numeric

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.

Resources