I would like convert my date format from dd-mm-yyyy to dd/mm/yyyy
Data:
date
1 22-Jul-2020
Current code:
format(as.Date(df$date, '%d:%m:%Y'), '%d/%m/%Y' )
[1] NA NA
Desired Output:
date
1 22/07/2020
The format in as.Date should match the input format. It is %d followed by -, then abbrevation for month (%b) followed by - and 4 digit year (%Y)
df$date <- format(as.Date(df$date, '%d-%b-%Y'), '%d/%m/%Y' )
df$date
#[1] "22/07/2020"
data
df <- structure(list(date = "22-Jul-2020"), class = "data.frame", row.names = "1")
You can try
library(lubridate)
df <- data.frame(date = c("22-Jul-2020"))
df$date <- dmy(df$date)
df$date <- format(df$date, format = "%d/%m/%Y")
# date
#1 22/07/2020
Related
Let's say if I have the following data frame.
date <- c('23/01/21 22:53','15/02/21 20:01', '05/03/21 07:49', '10/01/21 18:15', '09/03/21 12:53' )
id <- c(1:5)
df <- data_frame(id, date)
I tried to cast this from text to timestamp using the following code
df$date2 <- strptime(df$date, "%d/%m/%Y %H:%M")
This is the result that I get
The year is now showing 0021 instead of 2021. Is there a way I can show the right year?
Much appreciated with any help.
You could do a replacement to convert the two digit year to the correct four digit value:
date <- c('23/01/21 22:53','15/02/21 20:01', '05/03/21 07:49', '10/01/21 18:15', '09/03/21 12:53')
date <- paste0(substr(date, 1, 6),
ifelse(as.numeric(substr(date, 7, 8)) < 30, "20", "19"),
substr(date, 7, 14))
df <- data.frame(c(1:5), date)
df$date2 <- strptime(df$date, "%d/%m/%Y %H:%M")
For example: df$Date
SN Date
1 07-Mar-2019
2 06-Feb-2019
how do I set a condition to replace the value "Mar" = "03" and "Feb" = "02" in df$Date?
So that the output will be:
SN Date
1 07-03-2019
2 06-02-2019
anyone can help? Thank you
You can use as.Date. You can read about different formats at ?strptime
df$Date <- as.Date(df$Date, "%d-%b-%Y")
df
# SN Date
#1 1 2019-03-07
#2 2 2019-02-06
Or if you don't want to worry about format use dmy from lubridate
df$Date <- lubridate::dmy(df$Date)
Or anydate function from anytime.
df$Date <- anytime::anydate(df$Date)
To get output exactly in the same format as shown, we can do
df$Date <- format(as.Date(df$Date, "%d-%b-%Y"), "%d-%m-%Y")
df
# SN Date
#1 1 07-03-2019
#2 2 06-02-2019
data
df <- structure(list(SN = 1:2, Date = structure(2:1, .Label = c("06-Feb-2019",
"07-Mar-2019"), class = "factor")), class = "data.frame", row.names = c(NA, -2L))
Id. Int. Date.
234. 10. 10-05-2018
345. 05. 15-05-2018
564. 04. 17-06-2018
DF <- read.csv(file)
str(df)
I found date is in factor, so I want another column next to Date column with those date but in date format.
df$dte <- as.Date(df$Date, format= "%d/%b/%Y")
But I got a column next to Date called "date"column but the values are <NA>.
Kindly help me.
Changing df$dte <- as.Date(df$Date, format= "%d/%b/%Y") to df$dte <- as.Date(df$Date, format= "%d-%m-%Y") .
This question already has answers here:
Changing date format in R
(7 answers)
Closed 3 years ago.
Given two dates in a data frame that are in this format:
df <- tibble(date = c('25/05/95', '21/09/18'))
df$date <- as.Date(df$date)
How can I convert the dates into this format - date = c('1995-05-25', '2018-09-21') with the year appearing first and in four digit format, and by only using base R?
Here is my attempt, I successfully reversed the order, but still wasn't able to express the year in 4 digit format:
df <- tibble(date_orig = c('25/05/1995', '21/09/2018'))
df$date <- as.Date(df$date_orig)
year_date <- format(df$date, '%d')
month_date <- format(df$date, '%m')
day_date <- format(df$date, '%y')
df$newdate <- as.Date(paste(paste(year_date, month_date, sep = '-'), day_date, sep = '-'))
df$newdate_final <- as.Date(df$newdate, '%Y-%m-%d')
You need to know which format your date follows and find it in ?strptime to convert it in date object. As you required output is the standard way to represent dates you would not need format.
as.Date(df$date, "%d/%m/%Y")
#[1] "1995-05-25" "2018-09-21"
I want to add hours to day specified dates. And I want the output to be in date format. I wrote the below code:
day<-as.Date(c("20-01-2016", "21-01-2016", "22-01-2016", "23-01-2016"),format="%d-%m-%Y")
hour<-c("12:00:00")
date<-as.Date(paste(day,hour), format="%d-%m-%Y %h:%m:%s")
However, This code produces NA's:
> date
[1] NA NA NA NA
How can I do this in R? I will be very glad for any help. Thanks a lot.
The below code also doesn't work:
day<-as.Date(c("20-01-2016", "21-01-2016", "22-01-2016", "23-01-2016"),format="%d-%m-%Y")
time <- "12:00:00"
x <- paste(day, time)
x1<-as.POSIXct(x, format = "%d-%m-%Y %H:%M:%S")
It still prodeces NAs:
> x1
[1] NA NA NA NA
You can do either of these two:
dates <- as.Date(c("20-01-2016", "21-01-2016", "22-01-2016", "23-01-2016"), format = "%d-%m-%Y")
time <- "12:00:00"
x <- paste(dates, time)
as.POSIXct(x, format = "%Y-%m-%d %H:%M:%S")
dates <- c("20-01-2016", "21-01-2016", "22-01-2016", "23-01-2016")
time <- "12:00:00"
x <- paste(dates, time)
as.POSIXct(x, format = "%d-%m-%Y %H:%M:%S")
I personally find the second version simpler.