as.Date / strptime format - r

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")

Related

Date Format in R studio

my data is curently in this format
head(month)
[1] "192512" "192601" "192602" "192603" "192604" "192605
means 1925 Dec, 1926 Jan, etc
How do I convert this value to "Dec1925"
Thanks in advance
You can use the as.POSIXct function for all kinds of date manipulations, these are well worth investigating. Unfortunately without a day included in the date, this returns NA. So, to use it you can first append some day "01" to the end of your number strings. Then, when reformatting to character, the day can be dropped again.
as.character(as.POSIXct(paste0(month,'01'),format='%Y%m%d'),format = '%b%Y')
You can use ?as.POSIXct to see more about the as.POSIXct function.
?strptime will give you a listing of all the format options.

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"

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

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"

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"

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