as.date coercion in R [duplicate] - r

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.

Related

Convert string to Date in R: month abbreviated "dd-mmm-YYYY" [duplicate]

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"

how to convert unarranged dates to the default date format in R [duplicate]

This question already has answers here:
Convert numeric to date
(2 answers)
Closed 6 years ago.
I'm working with hundreds of stores data with R.
And there is store open data.
But it's unarranged, for example "20061019".
I'd like to convert into "YYYY-MM-DD" format.
Plz teach me how to deal with it. thx :)
paste(substr(aa,1,4),"")
[1] "2006 " "2016 "
paste0(substr(aa,1,4),"-",substr(aa,5,6),"-",substr(aa,7,8))
[1] "2006-10-19" "2016-09-09"
We can use as.Date with format argument (from base R)
as.Date("20061019", format = "%Y%m%d")
#[1] "2006-10-19"
One of the ways you can do this is with lubridate ymd function like this
library(lubridate)
ymd("20061019")

Date parsing of character vector ("Month-Year") [duplicate]

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.

Convert string to data format [duplicate]

This question already has an answer here:
R convert character vector values to Date values
(1 answer)
Closed 7 years ago.
I have a large number of strings of the following format:
a <- "19260701"
I would like to convert these do the following date format:
1926-07-01
when I try:
as.Date(a, "%Y-%m-%d")
I get
NA
You need to put the format of a in the function as.Date() :
a <- "19260701"
as.Date(a,format="%Y%m%d")
[1] "1926-07-01"
a<- "19260701"
library(lubridate)
ymd(a)
[1] "1926-07-01 UTC"

Insert leading value into numeric column in R [duplicate]

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.

Resources