R: Character into Date Format, Input like YYYYMMDD - r

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)

Related

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

Converting characters to dates

In R, it seems like this should be obvious, but I'm having trouble. I have dates formatted as 1/1/00, 12/31/00, etc., where each part is abbreviated.
When I try to convert it to a date, I get this error:
> headlines$Date <- as.Date(headlines$Date)
Error in charToDate(x) :
character string is not in a standard unambiguous format
I've also tried the below, but get all NAs:
> headlines$Date <- as.Date(headlines$Date,format="%b/%d/%y")
How should I convert this column to dates?
You were on the right track by adding the format argument. I'm guessing you realized that the first error ("character string is not in a standard unambiguous format") happened because R doesn't know which of the numbers is the day, month, or the year. Say one of the values was "01/02/03"; there's no way of knowing whether it's 2 January 2003, or 1 February 2003, and so on.
In this case I think you just need to fix what you're passing to the format argument. %b is the symbol for abbreviated month in text form, not number form (e.g. "Jan" instead of "01"). You need to use %m instead for months stored as numbers. Try this:
headlines$Date <- as.Date(headlines$Date,format="%m/%d/%y")
See this page for more info about date formats in R.
Replace format = "%b/%d/%y" with format = "%m/%d/%y".
%b means month as in Jan, Feb, Mar and so on.
%m is the integer equivalent (1, 2, 3 etc).
Further reading: https://www.stat.berkeley.edu/~s133/dates.html

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.

How to format dates in 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.

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