Convert string to data format [duplicate] - r

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"

Related

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"

How to convert character into date format? [duplicate]

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"

How to convert a numeric form to date form [duplicate]

This question already has answers here:
Convert numeric to date
(2 answers)
Closed 3 years ago.
I am trying to convert the field which is numeric in my dataset df to a date format using R.
The value is present as sear_date in the dataset and is in format "20181212".
However, I would like to convert this field from numeric to date in R
df$search_date=as.Date(df$search_date,origin = "1970-01-01"))
I expect the output to just change the type of the variable from numeric to date. However, it changes the fields themselves to wrong data
Use as.Date with the indicated format. If it is numeric convert to character first.
as.Date("20181212", format = "%Y%m%d")
## [1] "2018-12-12"
as.Date(as.character(20181212), format = "%Y%m%d")
## [1] "2018-12-12"

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"

Retrieving date from sequence does not give the value [duplicate]

This question already has answers here:
Looping over a Date or POSIXct object results in a numeric iterator
(7 answers)
Closed 9 years ago.
I'm trying to run the following code in R
> mySeq <- seq(as.Date("2012-1-1",format = "%Y-%m-%d"),
as.Date("2012-1-3",format = "%Y-%m-%d"),
by="1 day")
> for (i in mySeq){print(i)}
And I get:
[1] 15340
[1] 15341
[1] 15342
but mySeq[1] returns "2012-01-01"
Why? what am I missing here?
Your date values are converted to numeric in for() function to use them as index values.
Instead you can use seq_along() to get index values and then print mySeq[i].
for(i in seq_along(mySeq)) {print(mySeq[i])}

Resources