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

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"

Related

how to convert date in multiple character format into one standerd date format [duplicate]

This question already has answers here:
How can I fix corrupted dates in R?
(1 answer)
R datetime series missing values
(3 answers)
Closed 4 months ago.
I have a date file which has date in following format:
2017-01-04 00:00:00 (Y-d-m)
2017-03-22 00:00:00 (Y-m-d)
06/04/2017 (d/m-Y)
When I import this into R it takes it as a character. I want to convert it into Date format,but when I convert it into date format it takes some NA values
I have tried this:
df_TEST$UTC_date <- as.Date(ifelse(grepl("-", df_TEST$UTC_date),
as.Date(df_TEST$UTC_date, format = c("%Y-%m-%d")),
as.Date(df_TEST$UTC_date, format = c("%d/%m/%Y"))), origin = "1970-01-02")
but it does 't make a distinguish between (Y-d-m) and (Y-m-d). How can I convert these dates into one date format in R?

Convert a large character string to dates, but the dates have a non numeric character [duplicate]

This question already has answers here:
Parse string with additional characters in format to Date
(3 answers)
Closed 1 year ago.
I have a character vector in r that I obtained from a data.frame, which contains the dates of said data.frame. But there's an "x" at the beginning of each date. It looks like this (its just a part of the vector):
> dates_MODIS
[1] "X2015.01.01" "X2015.01.17" "X2015.02.02"
[4] "X2015.02.18" "X2015.03.06" "X2015.03.22"
[7] "X2015.04.07" "X2015.04.23" "X2015.05.09"
[10] "X2015.05.25" "X2015.06.10" "X2015.06.26"
...
I'm planning in converting this character vector into a date object, in order to use it in a new data.frame, but I can't convert it with as.Date() because of that "x" in the dates.
Any idea of how I can delete that "x" from every date, or another way of converting my character string into a date format?
You can use :
dates_MODIS <- c("X2015.01.01", "X2015.01.17", "X2015.02.02")
dates_MODIS <- as.Date(dates_MODIS, 'X%Y.%m.%d')
dates_MODIS
#[1] "2015-01-01" "2015-01-17" "2015-02-02"

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"

Converting YY/MM/DD to MM/DD/YY in R [duplicate]

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
>

Converting string formatted date to as.Date dd/mm/yyyy to dd-mm-yyyy in r [duplicate]

This question already has answers here:
Convert date-time string to class Date
(4 answers)
Convert string to date, format: "dd.mm.yyyy"
(4 answers)
Closed 2 years ago.
still fairly new to r. I have searched through the forum on different solutions that don't work on the data set that i have and what output i'm trying to achieve.
I have a column containing dates in the format dd/mm/yyyy (WorkoutDay) and would like to format it to dd-mm-yyyy.
running as.date function through the column gives me this output:
## example date in on of the columns "20/03/2020"
rd$WokoutDay <- as.Date(rd$WorkoutDay)
##output 0031-03-20
I tried to run a format function through but I get this instead which is also not what I'm after.
rd$WorkoutDay <- as.Date(rd$WorkoutDay) %>%
format("%d-%m-%y")
## output 20-03-31
I've read that maybe strptime or as.POSIXct might help but not sure how to use them
thanks in advance :)

Resources