How to convert character into date format? [duplicate] - r

This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 2 years ago.
I've got a character like this:
"2008-11" and I want to convert it in a date format. (only year and month)
I've already tried with zoo package:
yearmon("2008-11")
but it returns a NUM. I want a Date as structure. It should return 2008-11.
Which is the fastest way?
Thanks

library(anytime)
a <- anydate("2008-11")
> class(a)
[1] "Date"
> a
[1] "2008-11-01"

Related

Convert character into date format [duplicate]

This question already has answers here:
Convert a yyyymm format to date in R? [duplicate]
(1 answer)
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 8 months ago.
I currently have a database with dates in character format yyyy-mm format. e.g. 2009-01 and missing values.
I'm struggling to convert this into a date format.
You could use ym from lubridate package:
lubridate::ym('2009-01')
#[1] "2009-01-01"

How convert numeric value to date in R (same as Short date in excel)? [duplicate]

This question already has answers here:
How to convert Excel date format to proper date in R
(5 answers)
Closed 2 years ago.
I want to convert numeric date format to date. I.E. I have value 44109, but want to convert it to 2020-10-06. How is it possible?
Try something like below
> as.Date(44109,origin = "1900-01-01")
[1] "2020-10-07"
this works as well
x <- as.POSIXct(44109 * (60*60*24), origin="1899-12-30", tz="GMT")
"2020-10-05 GMT"

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"

Date format years-months in R [duplicate]

This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 9 years ago.
I have a data in the following format 200101 and I want it to be in the following format 2001-01 or 2001/01
Thanks
I don't deal with dates so there may well be better approaches. Your problem is you have no day. I know the zoo package can handle this but not in the format you want. I also give a regex approach but this is not a date class, just character.
As date:
library(zoo)
as.yearmon("200101", "%Y%m")
## > as.yearmon("200101", "%Y%m")
## [1] "Jan 2001"
As character:
gsub("([0-9]{4})","\\1-", "200101")
## > gsub("([0-9]{4})","\\1-", "200101")
## [1] "2001-01"
## gsub("([0-9]{4})","\\1/", "200101")

Converting dates with a format of d/mm/yy (for years earlier than 69) [duplicate]

This question already has answers here:
Add correct century to dates with year provided as "Year without century", %y
(3 answers)
Closed 9 years ago.
Converting the character "6/07/69" into a date using the as.Date function results in "2068-07-06" instead of "1968-07-06". How can I fix this?
Example:
as.Date(c("6/07/68", "6/07/69"), format="%d/%m/%y")
[1] "2068-07-06" "1969-07-06"
you can use library chron
e.g.
> library(chron)
> as.Date(chron(c("6/07/67", "6/07/69"), format = c(dates = "m/d/y")))
#[1] "1967-06-07" "1969-06-07"

Resources