How to convert a column in data.frame from characters to POSIXct? - r

I am trying to convert a column of date to POSIXct form. However, all the data here are not able to use as.POSIXct to convert since the day of the date are included. I tried using gsub(".* Friday, .*","",data) to remove all "Friday" but it is not working. What can I do here? Thank you. I tried to search this kind of problem but I didn't get a satisfied answer.

Directly with lubridate:
library(lubridate)
mdy("Friday, December 7, 1787")
[1] "1787-12-07"
But, POSIXct requires time and you don't have it. Therefore your class will be "Date".
If you really want a POSIXct then:
mdy_hms(paste("Friday, December 7, 1787", "00:00:00" ))
"1787-12-07 UTC"

Related

How to make B.C. Date format in R

I have string like 1/1/-2150. How to make Date format from that in R
lubridate back:
library(lubridate)
dmy("1/1/-2150")
[1] "2150-01-01"
as.Date("1/1/-2150",format="%d/%m/%Y")
[1] NA
Now 1/1/-2150 have class character. I need same value but with class Date
Thanks
UPDATE
Something like that, but using lubridate if it possible
minus=as.numeric(dmy("1/1/-2150"))
x<-as.numeric(ymd("0000-1-1"))
dt=as.Date(x*2-minus,origin="1970-01-01")+days(1)
str(dt)
Date[1:1], format: "-2150-01-01"
We need to specify the - in the format
as.Date("1/1/-2150",format="%d/%m/-%Y")
#[1] "2150-01-01"
Unfortunately there aren't any really large R packages tackling this issue (maybe no one has asked). The gregorian package should however, be able to tackle your BCE needs.
gregorian::as_gregorian("-2150-1-1")
[1] "Tuesday January 1, 2151 BCE"

Formatting Unconventional Date

I'm having trouble formatting a list of dates in R. The conventional methods of formatting in R such as as.Date or as.POSIXct don't seem to be working.
I have dates in the format: 1012015
using
as.POSIXct(as.character(data$Start_Date), format = "%m%d%Y")
does not give me an error, but my date returns
"0015-10-12" because the month is not a two digit number.
Is there a way to change this into the correct date format?F
The lubridate package can help with this:
lubridate::mdy(1012015)
[1] "2015-01-01"
The format looks ambiguous but the OP gave two hints:
He is using format = "%m%d%Y" in his own attempt, and
he argues the issue is because the month is not a two digit number
This uses only base R. The %08d specifies a number to be formatted into 8 characters with 0 fill giving in this case "01012015".
as.POSIXct(sprintf("%08d", 1012015), format = "%m%d%Y")
## [1] "2015-01-01 EST"
Note that if you don't have any hours/minutes/seconds it would be less error prone to use "Date" class since then the possibility of subtle time zone errors is eliminated.
as.Date(sprintf("%08d", 1012015), format = "%m%d%Y")
## [1] "2015-01-01"

as.Date / strptime format

What could be the problem? I don't seem to get why this had to be NA.
as.Date("jan2012", format="%b%Y")
[1] NA
I have also used strptime function and it is the same thing. I have been using this functions but I don't know they are not working this morning. Any insight as to why this is will be useful.
The Date include day also. So, we need to paste with a day i.e. 01
as.Date(paste("jan2012", "01"), format="%b%Y%d")
#[1] "2012-01-01"
"jan2012" isn't a date, it's a month. You need to prefix the day you want, e.g.
as.Date(paste0("01", "jan2012"), format = "%d%b%Y")

converting mddyy date to date

I have a date column in a .csv spreadsheet generated in inquisit.
the format is mddyy, so the 13th of July is 71315. R recognizes this as an integer.
Can anyone recommend a way to convert this to ISO 8601 date format?
Since you mention that the day is always two numbers, you could use a little sprintf() magic to add zeros out front.
as.Date(sprintf("%06d", 71315), "%m%d%y")
# [1] "2015-07-13"
The sprintf() call here adds zeros up to 6 characters and also turn it into a character vector, so as.Date() will accept it.
sprintf("%06d", 71315)
# [1] "071315"
We could also use mdy from library(lubridate)
library(lubridate)
mdy(71315) #returns POSIXct class.
#[1] "2015-07-13 UTC"
as.Date(mdy(71315)) #convert to `Date` class.
#[1] "2015-07-13"

How do I get my dates in standard unambiguous formats if they can't be recognized due to their ambiguous format?

I want to split up a Timestamp from Excel into year and julian day. I know, duplicate question, but combining everything I have found from other questions is not helping me.
The timestamp is formatted 1/13/2011 13:55 . So, I wanted to tell R to recognize this as a time variable. I have hours and minutes so I tried as.POSIXct and as.POSIXlt. These didn't work. I tried adding strptime --
as.POSIXct(strptime(df$TIMESTAMP, "%d/%m/%Y %H:%M%S"))
I just got NAs.
Once I got R to recognize it as a date, I was going to use lubridate like day(df$Date).
It seems as though you have month and day reversed
1/13/2011 13:55
with
"%d/%m/%Y %H:%M%S"
corresponds to the 1st day of the 13th month, which is probably why you're getting NAs. This seems to work for me:
a <- "01/13/2011 13:55"
t <- strptime(a, "%m/%d/%Y %H:%M")
t
"2011-01-13 13:55:00"

Resources