Say I want to store 1 February, 2003 as a date using a day-month-year without century format and I do the following:
> date <- as.Date("01-02-03", format = "%d-%m-%y")
And then I forget what format that date was in, and do this:
> date
[1] "2003-02-01"
In order to determine if 01 is the month or the day, I can do this:
> format(date, "%d")
[1] "01"
But is there a way to directly retrieve the format this date was stored in?
R only stores dates Date objects in one format. And technically it's a numeric format. The value is only formatted to look like a date when you print() the value. There is no formatting stored with the Date object. For example
dput(as.Date("2003-02-01"))
# structure(12084, class = "Date")
Dates are always printed yyyy-mm-dd ISO style when using the default print method. You can change that by using format() or strftime(). But the internal representation is always the same.
Related
I want to transform my character line into a Date Format to plot my Data. I Tried:
a`s.Date(IBM_REK$V1,"%YYYY%MM%DD", optional =FALSE)
I get only Na's.
The Data is imported from a text file and Stored in a data-frame. The first line is the date (V1):
$ V1: chr "19260130" "19260227" "19260331" "19260430"
I tried other codes like strp.time(), but no code works out.
strptime will do the job here. First of all, use ?strptime to see what kinds of format you want to use.
Let's say you have a list of dates:
dates = c("19260130", "19260227", "19260331", "19260430")
where format seems to be YYYYMMDD.
Using strptime:
strptime(dates[1], "%Y%m%d")
[1] "1926-01-03"
where %Y is 4 digit year, %m is decimal number of months, %d is day.
Try with below date format.
as.Date(IBM_REK$V1,"%Y%m%d", optional =FALSE)
I'm importing time series data from a CSV file and one of the vectors/columns are dates in the format DD/MM/YYYY. Vector class is characters or factors if I chose the Strings as factors = True. I convert the imported file to a data frame and then run the following:
df$Date <- as.Date(df$Date , "%d/%m/%y")
I get no error message, but the dates are all messed up in the format YYYYMMDD and all the YYYY are the year 2020...
Before:
10/09/2009
11/09/2009
14/09/2009
After:
2020-09-10
2020-09-11
2020-09-14
You are using %y when it should be %Y. See the documentation here.
%y
Year without century (00–99). On input, values 00 to 68 are prefixed by 20 and 69 to 99 by 19 – that is the behaviour specified by the 2004 and 2008 POSIX standards, but they do also say ‘it is expected that in a future version the default century inferred from a 2-digit year will change’.
%Y
Year with century. Note that whereas there was no zero in the original Gregorian calendar, ISO 8601:2004 defines it to be valid (interpreted as 1BC): see http://en.wikipedia.org/wiki/0_(year). Note that the standards also say that years before 1582 in its calendar should only be used with agreement of the parties involved.
Try running the code again so that the data frame is not modified by any previous attempt but this time use
df$Date <- as.Date(df$Date , "%d/%m/%Y")
#Heroka is right.
If ever you need it you could also use posixct objects (they contain information of seconds)
Try this:
df$Date.time <- as.POSIXct(df$Date , format="%d/%m/%Y")
If you want the date and time in strings you can try the following:
df$Date.time <- format(as.POSIXct(df$Date , format="%d/%m/%Y"),format="%Y-%m-%d %H:%M")
or
df$Date <- format(as.POSIXct(df$Date , format="%d/%m/%Y"),format="%Y-%m-%d")
I have log files where the date is mentioned in the ordinal date format.
wikipedia page for ordinal date
i.e 14273 implies 273'rd day of 2014 so 14273 is 30-Sep-2014.
is there a function in R to convert ordinal date (14273) to (30-Sep-2014).
Tried the date package but didn come across a function that would do this.
Try as.Date with the indicated format:
as.Date(sprintf("%05d", 14273), format = "%y%j")
## [1] "2014-09-30"
Notes
For more information see ?strptime [link]
The 273 part is sometimes referred to as the day of the year (as opposed to the day of the month) or the day number or the julian day relative to the beginning of the year.
If the input were a character string of the form yyjjj (rather than numeric) then as.Date(x, format = "%y%j") will do.
Update Have updated to also handle years with one digit as per comments.
Data example
x<-as.character(c("14273", "09001", "07031", "01033"))
Data conversion
x1<-substr(x, start=0, stop=2)
x2<-substr(x, start=3, stop=5)
x3<-format(strptime(x2, format="%j"), format="%m-%d")
date<-as.Date(paste(x3, x1, sep="-"), format="%m-%d-%y")
You can use lubridate package as follows:
>library(lubridate)
# Create a template date object
>date <- as.POSIXlt("2009-02-10")
# Update the date using
> update(date, year=2014, yday=273)
[1] "2014-09-30 JST"
I'm having trouble changing date format in R. I have a vector "StartDate" with dates and time for instance in the format:
01Feb1991 00:00
I did:
as.POSIXct(as.character(bio$StartDate), format = "%d/%m/%Y %H:%M")
...but I got NAs as a result. Would there be a different way to change the vector into date format?
The format you provide has to match your string. In your case, that's '%d%b%Y %H:%M' (you don't have slashes between day, month and year, and your month is the abbreviated name, not the number).
as.POSIXct('01Feb1991 00:00', format='%d%b%Y %H:%M')
See ?strptime (mentioned in ?as.POSIXct) for various tokens you can use for dates.
I am running into some date issues when working with Dates in R.
Here's my situation-
I have a data set based on dates and finally got the Date field converted from character to Date in R using the following code
o1$Date <- as.Date(o1$Date , "%m/%d/%y")
(My dataset is o1 and Date is the name of my Date column)
My Date column has the following values
"1/1/2013" "1/1/2014" "1/10/2013" "1/10/2014" "1/11/2013" "1/11/2014"
However when I convert the Char to Date I get the following Dates
"2020-01-01" "2020-01-01" "2020-01-10" "2020-01-10" "2020-01-11"
Any suggestions on what the problem could be and how to work around it?
look at ?strptime to see the formatting options for times and dates. You need to use %Y rather than %y which is for a 2 digit year.