I have a dataset with a date format that is below. I want to:
1. strip the time off the date column, so that it becomes 2/06/2020
or better yet 2Jun2020
2. then I want to make my column Date
into date format, so that I can subset off it
> print(df$Date)
[1] "2/06/2020 0:00" "12/06/2020 0:00" "12/06/2020 0:00" "29/06/2020 0:00" "3/06/2020 0:00" "25/06/2020 0:00" "25/06/2020 0:00"
[8] "25/06/2020 0:00" "26/06/2020 0:00" "18/06/2020 0:00" "3/06/2020 0:00" "4/06/2020 0:00" "10/06/2020 0:00" "10/06/2020 0:00"
> summary(df$Date)
Length Class Mode
16749 character character
I tried this but it returned all NA's
:
df$Date2 <- as.Date(df$Date, format = "%d-%m-%Y %H:%M")
Does this work:
d <- c("2/06/2020 0:00", "12/06/2020 0:00", "12/06/2020 0:00", "29/06/2020 0:00")
as.Date(gsub('(.*)(\\s\\d:\\d{2})','\\1',d),format = '%d/%m/%Y')
[1] "2020-06-02" "2020-06-12" "2020-06-12" "2020-06-29"
If you want it in 02/06/2020
format:
format(as.Date(gsub('(.*)(\\s\\d:\\d{2})','\\1',d),format = '%d/%m/%Y'),'%d/%m/%Y')
[1] "02/06/2020" "12/06/2020" "12/06/2020" "29/06/2020"
building on previous answer:
d = "2/06/2020 0:00"
x = format(as.Date(gsub('(\\s\\0(\\d):\\0(\\d){2})','\\1',d),format = '%d/%m/%Y'),'%d/%m/%Y')
gsub("^0+(\\d)", "\\1", x)
# [1] "2/06/2020"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.