Converting characters to dates - r

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

Related

Trouble obtaining quarterly values from a date variable in stata

I am starting with a date_of_survey variable that is a string formatted as YYYY-MM-DD. I then run the following commands to convert it to a date variable, and display that variable in a useful format:
gen date = date(date_of_survey, "YMD")
gen date_clean = date
format date_clean %dM_d,_CY
drop date_of_survey
That leaves me with a "date_clean" variable displayed as "September 3, 2020" and a corresponding "date" variable displayed as "22161" (equal to days since January 1, 1960).
I now need to create a variable that indicates the year and quarter of each observation, preferably in YYYY-QQ format. I assumed this shouldn't be difficult, but no matter how I have coded it, I wind up with years in the 7000s and inaccurate quarters. I must be misunderstanding how the dates are stored. My first instinct was to try a simple format date %tq command, but I'm still not getting the output I need. Any help is much appreciated. I read over the help files, and can't find the discrepancy that's causing this little problem.
ANSWER: I needed to put the date variable into quarters since January 1, 2021.  a qofd() function call before the format %tq did the trick!

controlM variable for YYYYMM?

I'm using ControlM and in a command, I would like to find a variable that gives me the date in this format : YYYYMM
I found there is %%$DATE variable but it gives YYYYMMDD
Thanks for you help
It is possible to define and concatenate a variable that will represent the date in such a format.
These are available:
Day DD, %%DAY,
Month MM, %%MONTH,
Year YY, %%YEAR,
Year YYYY, %%$YEAR
Prefer %%$OYEAR AND %%OMONTH over %%$YEAR and %%MONTH
I suggest using the variables %%$OYEAR and %%OMONTH over %%$YEAR and %%MONTH. The reason is that date variables beginning with O refer to processing dates and do not necessarily coincide with the system date. For this case you could use any of the following options:
1. YYYYMM = %%$OYEAR.%%OMONTH
2. YYYYMM = %%SUBSTRING %%$ODATE 1 6
The $ symbol preceding the variable %%$OYEAR or %%$ODATE indicates that the year is returned in 4-digit format, instead of OYEAR or ODATE which print the year with only 2 digits.
The dot (.) character is used for concatenate variables.
For example: For the order day May 29, 2020.
1. %%$ODATE would print 20200529
2. %%ODATE would print 200529

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 convert character to date in r with February as the month?

I am trying to convert some characters into date formats. I have so far used as.Date, anytime, sprtime,as.POSIXct. For all of these functions it is possible to convert the characters to dates except when it comes to the second month of the year (February). For example, when I use as.Date("1978/2/30") I get the following error message:Error in charToDate(x) :
character string is not in a standard unambiguous format
. Then, if a specify the format, I get NAs: as.Date("1978/2/30","%Y/%m/%d"). What is curious is that for any other month of the year it works perfectly:as.Date("1978/3/30","%Y/%m/%d") gives as.Date("1978/3/30","%Y/%m/%d"). Does anyone understand what I am missing?

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)

Resources