I have a variable called datetime with values that look like this 2020-05-30 20:12:23 and I want to convert them into just 2020-05-30. In other words, I want to get rid of the time value.
I am trying to use the as.Date function, but that does not seem to be working.
Any tips would be appreciated.
CodePudding user response:
library(tidyverse)
tibble(
date = "2020-05-30 20:12:23"
)
Sample data
# A tibble: 1 x 1
date
<chr>
1 2020-05-30 20:12:23
Convert to date without time
df %>%
mutate(date = as.Date(date, format = "%Y-%m-%d"))
# A tibble: 1 x 1
date
<date>
1 2020-05-30
CodePudding user response:
Maybe the following is more dplyr
-idiomatic than the OP's answer.
suppressPackageStartupMessages(library(dplyr))
query1 <- data.frame(datetime = c("2021-04-30 19:20:27", "2020-05-30 20:12:23"))
datetimedf <- query1 %>%
mutate(date = as.Date(datetime, tz = '')) %>%
select(date)
datetimedf
#> date
#> 1 2021-04-30
#> 2 2020-05-30
Created on 2022-06-13 by the reprex package (v2.0.1)
CodePudding user response:
Looks like this worked:
My code:
datetimedf <- query1 %>% subset(select = c(datetime))
datetimedf <- data.frame(datetimedf)
datetimedf <- data.frame(as.Date(datetimedf$datetime, tz = ''))