Home > OS >  Removing Time from Date Time Column in R
Removing Time from Date Time Column in R

Time:11-18

How do I remove Time from Date-Time columns in the dataframe mentioned below:

> structure(list(hhenrid = c(2L, 3L, 4L, 5L, 6L, 7L, 8L, 10L, 11L, 
12L, 13L, 14L, 15L, 18L, 19L, 20L, 21L, 22L, 23L, 24L), dob = c("1/1/1970 12:00:00 AM", 
"1/1/1976 12:00:00 AM", "1/1/1983 12:00:00 AM", "1/1/1980 12:00:00 AM", 
"12/31/1966 12:00:00 AM", "1/1/1990 12:00:00 AM", "1/1/1993 12:00:00 AM", 
"1/1/1959 12:00:00 AM", "12/31/1984 12:00:00 AM", "12/31/1969 12:00:00 AM", 
"12/31/1968 12:00:00 AM", "1/1/1979 12:00:00 AM", "12/31/1958 12:00:00 AM", 
"12/31/1986 12:00:00 AM", "1/1/1978 12:00:00 AM", "12/31/1987 12:00:00 AM", 
"12/31/1987 12:00:00 AM", "12/31/1967 12:00:00 AM", "12/31/1993 12:00:00 AM", 
"1/1/1953 12:00:00 AM"), voedate = c("7/28/2018 12:00:00 AM", 
"7/24/2018 12:00:00 AM", "7/24/2018 12:00:00 AM", "8/3/2018 12:00:00 AM", 
"7/22/2018 12:00:00 AM", "8/3/2018 12:00:00 AM", "7/23/2018 12:00:00 AM", 
"7/24/2018 12:00:00 AM", "7/10/2018 12:00:00 AM", "7/30/2018 12:00:00 AM", 
"7/30/2018 12:00:00 AM", "7/20/2018 12:00:00 AM", "7/24/2018 12:00:00 AM", 
"7/27/2018 12:00:00 AM", "7/31/2018 12:00:00 AM", "7/18/2018 12:00:00 AM", 
"7/27/2018 12:00:00 AM", "8/1/2018 12:00:00 AM", "8/1/2018 12:00:00 AM", 
"8/1/2018 12:00:00 AM")), row.names = c(NA, 20L), class = "data.frame")

I am trying this code to remove Time from Date-Time Columns:

hh111$dob<-format(as.POSIXct(hh111$dob, format = '%d/%m/%Y %H:%M:%S'), format = '%d/%m/%Y')

But it shows only the dates which has 01/01/YYYY and NA for other values. Need help to remove Time from both Date-Time Columns i.e. dob and voedate.

CodePudding user response:

One way would be to use str_split:

library(tidyverse)
dat %>% mutate(across(c(dob, voedate), ~str_split(., " ", simplify=T)[,1]))
   hhenrid        dob   voedate
1        2   1/1/1970 7/28/2018
2        3   1/1/1976 7/24/2018
3        4   1/1/1983 7/24/2018
4        5   1/1/1980  8/3/2018
5        6 12/31/1966 7/22/2018
6        7   1/1/1990  8/3/2018
7        8   1/1/1993 7/23/2018
8       10   1/1/1959 7/24/2018
9       11 12/31/1984 7/10/2018
10      12 12/31/1969 7/30/2018
11      13 12/31/1968 7/30/2018
12      14   1/1/1979 7/20/2018
13      15 12/31/1958 7/24/2018
14      18 12/31/1986 7/27/2018
15      19   1/1/1978 7/31/2018
16      20 12/31/1987 7/18/2018
17      21 12/31/1987 7/27/2018
18      22 12/31/1967  8/1/2018
19      23 12/31/1993  8/1/2018
20      24   1/1/1953  8/1/2018

CodePudding user response:

Turning them into dates will strip the time as well:

library(dplyr)
df1 %>% 
  mutate(across(c(dob, voedate), ~ as.Date(., format("%m/%d/%Y"))))

   hhenrid        dob    voedate
1        2 1970-01-01 2018-07-28
2        3 1976-01-01 2018-07-24
3        4 1983-01-01 2018-07-24
4        5 1980-01-01 2018-08-03
5        6 1966-12-31 2018-07-22
6        7 1990-01-01 2018-08-03
7        8 1993-01-01 2018-07-23
8       10 1959-01-01 2018-07-24
9       11 1984-12-31 2018-07-10
10      12 1969-12-31 2018-07-30
11      13 1968-12-31 2018-07-30
12      14 1979-01-01 2018-07-20
13      15 1958-12-31 2018-07-24
14      18 1986-12-31 2018-07-27
15      19 1978-01-01 2018-07-31
16      20 1987-12-31 2018-07-18
17      21 1987-12-31 2018-07-27
18      22 1967-12-31 2018-08-01
19      23 1993-12-31 2018-08-01
20      24 1953-01-01 2018-08-01

CodePudding user response:

library(lubridate)

df$datetime <- lubridate::mdy_hms(df$voedate, tz="Europe/London")
  •  Tags:  
  • r
  • Related