This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 6 years ago.
I'm trying to parse a vector tsl into a date class.
tsl <- c("Dec-2011", "Dec-2011", "Dec-2011", "Dec-2011", "Dec-2011",
"Dec-2011")
I read the documentation of as.Date and I thought that as.Date(tsl, "%b-Y") should do the job, but it returns NAs.
I've also tried the following:
as.Date(tsl, "bbb-YYYY")
as.Date(tsl, "by")
Then I tried using lubridate but this didn't work either:
library(lubridate)
parse_date_time(tsl, "%b-%y")
Running Sys.getlocale() returns:
"LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United Kingdom.1252;LC_MONETARY=English_United Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252"
Any ideas how to solve it?
If you can live with year-month-day dates, here is a solution:
as.Date(gsub("^", "01-", tsl), format="%d-%b-%Y")
I added on the first of the month using gsub.
Related
This question already has answers here:
How to convert date to format "yyyy-mm-dd" in R when input value can be of different formats
(3 answers)
Change Date print format from yyyy-mm-dd to dd-mm-yyyy
(2 answers)
Closed 2 years ago.
I need to find the difference in days between 2 date columns, but one of them is in the format of "6/16/2019" and the other is in the format of "2019-02-25". Not sure which one would be easier to convert to which, but would like to get end result in days. Which I know how to do. I would appreciate help converting the second yyyy-mm-dd to mm-dd-yyyy.
We can use functions from the lubridate package to convert the different formats to dates, and then subtract.
rawData <- "date1,date2
2002-05-15,6/16/2019
2019-12-31,4/15/2020"
data <- read.csv(text = rawData,stringsAsFactors = FALSE)
library(lubridate)
mdy(data$date2) - ymd(data$date1)
...and the output:
> mdy(data$date2) - ymd(data$date1)
Time differences in days
[1] 6241 106
>
This question already has answers here:
date functions in R return wrong year
(2 answers)
Closed 3 years ago.
Within R, I'm trying to convert a text string into a Date variable type using lubridate's as.Date function.
I have a vector of values such as:
Dates
11/28/2019
11/29/2019
I am attempting to convert these to standard date variables using this as.Date function:
as.Date(Dates, "%m/%d/%y")
I do not receive an error message, and it correctly interprets the month and date, but for some reason it's outputting the wrong year - one year ahead:
"2020-11-28"
"2020-11-29"
I have no earthly idea why it is incorrectly interpreting the year in this way. Any help is appreciated!
We need to use %Y for 4 digit year as %y refers to only 2 digit
as.Date(Dates, "%m/%d/%Y")
Or using lubridate, this would be resolved
library(lubridate)
mdy(Dates)
Or with anydate from anytime
library(anytime)
anydate(Dates)
This question already has answers here:
strptime, as.POSIXct and as.Date return unexpected NA
(2 answers)
Closed 4 years ago.
I have a vector of strings which I need to convert to dates. This only works for some of the dates which are in the format of "dd-mmm-YYYY".
Example: this works: strptime("21-Sep-2017", format = "%d-%b-%Y")
This does not work and returns NA: strptime("21-Dec-2017", format = "%d-%b-%Y")
What am I doing wrong or not seeing?
This is because your locale is probably one where December is not abbreviated as Dec. Without changing your session settings, you could simply do
lubridate::parse_date_time("21-Dec-2017", orders = "d-b-Y", locale = "us")
[1] "2017-12-21 UTC"
This question already has answers here:
Changing to Date Format in R
(2 answers)
Closed 8 years ago.
Hello, I cannot figure out where the problem is even after consulting the documentation for as.Date, although it must be a very silly thing.
I have a character vector DD that contains dates:
unique(DD)
[1] NA "1999-01" "2013-08" "2013-02" "2013-03" "2013-01" "2013-07" "2012-12" "2013-05"
[10] "2012-11" "2013-04" "2013-06" "2011-11" "2011-12" "2011-06" "2010-07" "2011-01" "2010-03"
I want to convert it into date format. I tried
DD2 = as.Date(DD, format = "%Y-%m")
but the result gives only NAs :
unique(DD2)
[1] NA
Can anyone see the problem?
I'm not really sure why this doesn't work, but it appears you do have to completely specify the date for as.Date to work. Instead, you could use as.yearmon from the zoo package.
This question already has answers here:
How to add leading zeros?
(8 answers)
Closed 8 years ago.
this is likely really simple, but I am unable to find the answer (probably not searching for the right thing...)
How can I insert a lead 0 into a column of numeric values...
Example:
values <- c(91414, 80113)
Such that they appear like this afterward:
091414, 080113
Then I would like to transform to a date, and I assume I would just:
date <- as.Date(values, format = "%m%d%y")
Any and all help appreciated...
Try this, using the lubridate package for the date transformation:
X<-c(91414,80113)
X_date<-mdy(paste("0",as.character(X),sep=""))
X_date
[1] "2014-09-14 UTC" "2013-08-01 UTC"
This turns your numeric data to text and add a "0" to the the start of each value, then mdy does the date conversion.