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
>
Related
This question already has answers here:
Convert string to date, format: "dd.mm.yyyy"
(4 answers)
What are the "standard unambiguous date" formats for string-to-date conversion in R?
(8 answers)
Closed 3 months ago.
For my dataset, the Date variable has dates in the format of this example: 19-Feb-03
I want to change the above character format dates in the column to a Date format. (As I have to do time series analysis later on)
I tried using the as.Date() method but it didn't work.
x <- '19-Feb-03'
lubridate::ymd(x)
"2019-02-03"
Not sure whether 19 is year or day. You can try lubridate package
x<-"19-Feb-03"
library(lubridate)
ymd(x)
dmy(x)
This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 1 year ago.
I have a date that is in the this format:
chr [1:56] "Sep-2016" "Oct-2016" "Nov-2016" "Dec-2016" "Jan-2017" "Feb-2017" "Mar-2017" "Apr-2017"
I tried as.Date(Dates, "%b-%Y") and got NA for all the values. For some reason I have tried multiple ways and using different format but it is still not working. I am looking to get it into either 09-2016, 10-2016 or just simply turn it into a date format.
Any help is much appreciated!
Date class needs a day as well. Easiest is to convert to yearmon class from zoo and then coerce it to Date, which adds a dummy day
library(zoo)
as.Date(as.yearmon(Dates, '%b-%Y'))
or in base R, paste a day and convert
as.Date(paste0(Dates, '-01'), '%b-%Y-%d')
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 :)
This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 4 years ago.
I am trying to convert character strings to Dates in R. These are examples of the character strings:
"Aug-1973" "Aug-1974" "Aug-1975" "Aug-1976" "Aug-1977"
I run the following line on date strings similar to the ones above:
exportsDF$Date <- as.Date(as.character(exportsDF$Date), format = "%b-%Y")
This returns NAs for all values. The step where I convert the dates column to characters returns the correct values. Any ideas why the as.Date() command is not working? There are no NAs or missing values in the data. Every value has a "%b-%Y" format.
Any help is appreciated!
The date format needs a day as well, so you could add an arbitrary day of the month. Here, I've chosen the first day:
dates <- c("Aug-1973", "Aug-1974", "Aug-1975", "Aug-1976", "Aug-1977")
res <- as.Date(paste0("01-", dates), format = "%d-%b-%Y")
print(res)
#[1] "1973-08-01" "1974-08-01" "1975-08-01" "1976-08-01" "1977-08-01"
The reason is that the underlying Date data type is an integer counting the days since some reference day. Specifically, the number of days since 1970-01-01. See ?Date.
The Date object res can now be displayed as you please via
format(res, "%B-%Y")
#[1] "August-1973" "August-1974" "August-1975" "August-1976" "August-1977"
or similar.
The month(res) function and its cousins are also helpful. See ?month.
This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Get the difference between dates in terms of weeks, months, quarters, and years
(9 answers)
Closed 4 years ago.
I transformed a date in the yyyy-mm-dd format to the yyyy-mm format using the following command:
format(as.Date(DATE, "%Y-%m-%d"), "%Y-%m")
This works but it returns a character. I want to further use this as a date, so I want to transform this character back to a date class. Using the as.Date() function gives me the error:
Error in charToDate(x) : character string is not in a standard unambiguous format
Does anyone know how to solve this problem?
Update: In the end I want to determine the number of months between two dates in the format yyyy-mm. Does anyone knows how to do this without transforming the characters back to date class?
I had once the same problem and unfortunately the only solution I found was to keep format YYYY-mm-dd with dd==01...
Here is part of the code if you want:
DATE <- str_c(DATE,"-01") df$date <- as.Date(DATE,format="%Y-%m-%d")