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"
Related
How to convert a character column to date in R
My column is in the format "20110314" (exclude ") where first 4 characters refer to year, next two to month and the last two to date.
You can use lubridate: https://github.com/tidyverse/lubridate
Date-time data can be frustrating to work with in R. R commands for date-times are generally unintuitive and change depending on the type of date-time object being used. Moreover, the methods we use with date-times must be robust to time zones, leap days, daylight savings times, and other time related quirks, and R lacks these capabilities in some situations. Lubridate makes it easier to do the things R does with date-times and possible to do the things R does not.
Installation:
# The easiest way to get lubridate is to install the whole tidyverse:
install.packages("tidyverse")
# Alternatively, install just lubridate:
install.packages("lubridate")
# Or the the development version from GitHub:
# install.packages("devtools")
devtools::install_github("tidyverse/lubridate")
Usage:
ymd(20110314)
#> [1] "2011-03-14"
strptime(as.character(20110314),"%Y%m%d")
[1] "2011-03-14 PDT"
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"
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"
I'm currently working my way through the adehabitatLT package.
I've put my date_time column into characters and named it da:
da<-as.character(dat$date_time)
head(da)
[1] "7/08/2015 0:22" "7/08/2015 0:52" "7/08/2015 1:22" "7/08/2015 1:52" "7/08/2015 2:56" "7/08/2015 3:26"
As you can see my date_time input is a bit non traditional and i think this is where the error occurs, because when i create the class POSIXct:
da<-as.POSIXct(strptime(as.character(dat$date_time),"%d/%m/%y% H:%M:%S"))
It creates the class but i get NA for all my values:
head(da)
[1] NA NA NA NA NA NA
My end objective here is to create an object of the class ltraj (but not only containing the date but the time as well).
Any ideas anyone?
Kind regards,
Sam
da<-as.POSIXct(strptime(as.character(locs$Date),"%y%m%d"))
The format should be modified to
as.POSIXct(strptime(da, "%d/%m/%Y %H:%M"))
Or if month is first followed by day, then change it to "%m/%d/%Y %H:%M"
While parsing tricky date/time formats, it might be useful to use lubridate package by Garrett Grolemund and Hadley Wickham.
In your case, simply do
require(lubridate)
a <- dmy_hm(da)
The separator and the number of digits for day or month or hours etc are automatically parsed.
Find more info here
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")