convert to date in R using abbreviated month [closed] - r

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm new to R and trying to load some time series data but I'm stuck at the first hurdle.
I have a dataframe with a date column called Date. The date format of the data is: 23-May-16 (it appears like this in the R console when I print df). To read as date I'm trying:
df$Date <- as.Date(df$Date, "%dd-%bbb-%yy")
as per guidance here
which produces the value <NA> when it reads the data.

Try:
as.Date(df$Date,format="%d-%b-%y")

You only need to list those once:
as.Date("23-May-16", "%d-%b-%y")

Related

Change the date format in R [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Is there a way to change the date format in R. Example, when I tried with below formula to convert, it was not showing a right result
format(as.Date("30-01-2021"), "%Y/%d/%m")
"0030/20/01"
Expected output
"2021-01-30"
The lubridate package works really well for this kind of formatting.
#Load library
library('lubridate')
# Convert date format
lubridate::dmy("30-01-2021")
The reason it is not working is because your date string is not in the default date format. The default is "%Y-%m-%d" but your string is "%d-%m-%Y". So you can fix it by specifying the input date format on the as.Date() function. Like this:
as.Date("30-01-2021", "%d-%m-%Y")
# [1] "2021-01-30"

How do I convert a column of dates in integer format to date format in base R? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm working with a dataframe that has a column of dates, all of which are integers in the form 20210117 for January 17, 2021.
I tried to run
data$DATE <- as.Date(as.character(data$DATE), "%Y%m%d")
but this turns all my dates into NAs. What am I doing wrong and is there a way to fix this in base R without importing libraries?
You can use the format option when calling as.Date:
dates <- as.character(data$YearMonthDay)
data$newVar = as.Date(dates, format="%Y%m%d")
Note that you don't need to use the origin parameter here, because even though you are passing in numerical data, it is in the form of an actual text date. origin is used when passing in number of days since an epoch.
It is generally not advisable to go in this direction, and in any case, base R date's have to have a year component. So, include the year component, and if you don't want to use it, then don't use it.

for loop in data frame in R [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am new in R and I would like to ask what is wrong in the following simple command. When i try:
for (k in 1:1){
logdata= logDataset[1:74+k,k]}
logdata collects the values from rows 2:75 of column 1 of the logDataset data frame. But when I use:
logdata=logDataset[1:75,1]
logdata collects correctly the first 75 values of the first column. Why the first command does not work as the second one? How can I use the "for" command for collecting the first 75 values of the first column? Many thanks.

How to parse non standard date formate in R [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am trying to parse a date string like 1/12/2014 which represents the first of december 2014. However, looking at http://www.statmethods.net/input/dates.html it looks like R would only support parsing something like 01/12/2014. Is this correct? How could I parse the date mentioned above?
Update: I used y instead of Y for the year and the date got parsed as "2020-12-01" --> everything working fine now.
No.
as.Date("1/12/2014", format="%d/%m/%Y")
See the Details section of ?strptime for an extensive list of the components of date-time that R can format.
lubridate makes handling dates easy. Use dmy for dates in day-month-year format.
library(lubridate)
dmy("1/12/2014")

Date conversion in R not registering string correctly [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Can someone please tell me why this doesn't work:
as.Date("01/08/15", format = "%m/%d/%Y")
[1] "0015-01-08"
Thanks!
You need to use lower-case %y. Upper-case %Y requires a 4-digit year. (In my opinion, the entire command should have failed with an error message, but unfortunately it didn't.) See strptime(), which documents the format codes.
as.Date("01/08/15", format = "%m/%d/%y")
## [1] "2015-01-08"

Resources