How to format dates in R - r

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.

Related

Retrieving the format of an object of class 'Date' in R

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.

Subset month, day, hour, minute, second from a date [duplicate]

This question already has answers here:
strptime, as.POSIXct and as.Date return unexpected NA
(2 answers)
Changing date format in R
(7 answers)
Closed 1 year ago.
I have a column in a dataframe which contains dates eg:
Date <- as.Date("2012-12-01 00:00:00")
Note: the actual dataframe format is "unknown"
I want to subset the month, date, hour from this dataframe and used this code
dmH <- as.POSIXct(Date, format="%Y%m%d %H%M%s")
dmH <- format(dmH, format="%m%d %H%M%s")
which returns a character format as below
"1201 02001354320000"
During this process it changed from UTC to EET so it starts at 02:00:00 and I don't know how to omit this change.
Most importantly, I need to have it in date format to be able to use it in a ggplot but I wasn't able to find any way to convert it, no matter what and how I tried.
EDIT:
As #Cath mentioned in the comment I tried to use that code but as.Date function returns only the year, month, day without the time. As a result, when I then try format function for any other time of the day it returns "00".
As opposed to as.Date I used again as.POSIXct and now it returns the right format (since I used the hyphen and %S in the "format" argument as you recommended). But still this is in character format which I need in date format.
So I used again mdH <- as.POSIXct(mdH, format = "%m-%d %H:%M:%S") on the formatted dataframe(mdH) as well as strptime to change it to date format but both return also the current year.
Note that if I use directly dmH <- strptime(as.character(Date), format="%m-%d %H:%M:%S") (as in one of the threads you recommended) it returns NA. Am I missing something? I can't resolve my issue

R: Character into Date Format, Input like YYYYMMDD

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)

Converting from fctr to date format.

I am attempting to convert a column in my data set from fctr to date format. The current column has data formatted as follows: "01/01/14. 01:00 Am." Ideally I would like to create a column for day and then a column for time as well. There are periods following the day and the time which is another issue I am facing. So far I have attempted to use lubridate to create a new column of data but I get the error "All formats failed to parse. No formats found." Any help would be greatly appreciated, thank you.
test <- fourteen %>%
mutate(When = mdy_hms(V3))
View(test)
If your date factor literally has levels that look like 01/01/14. 01:00 Am. including two periods and a space between the first period and the first hour digits and a space between the minutes and the am/pm designation, and all the dates are in this format, then the following should work:
... mutate(When = as.POSIXct(V3, format="%m/%d/%y. %H:%M %p.")) ...
In particular, the following standalone testcase works fine:
as.POSIXct(factor("01/01/14. 01:00 Am."), format="%m/%d/%y. %H:%M %p.")
For more information on the format argument being used here, see the R help page for the function strftime.

Convert from character to Date

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.

Resources