removing part of a string value in a data frame based on the location of comma

Hello everyone,

I have the following example data Frame:

Date <- c("9.3.2020, 18:18", "10.4.2020, 20:13", "20.12.2020, 9:15")
JumpHeight <- c(35.1, 29.5, 36.2) 
BodyWeight <- c(90.6, 90.5, 91.7)
df <- c(Date, JumpHeight, BodyWeight)

In this data Frame, the "Date" variable is unfortunately written as the date (d.m.y), time (hh:mm). I am not interested in the time aspect of this column and would like to remove the time so I can format this date properly using the as.Date function.

I am having a difficult time removing the time in this column. I would like to remove everything including and after the comma in this data string.

Thank you for anyone who is willing to help!


the_time <- "\\d+:\\d{2}"

dat <- data.frame(Date = c("9.3.2020, 18:18", "10.4.2020, 20:13", "20.12.2020, 9:15"),
  JumpHeight = c(35.1, 29.5, 36.2),
  BodyWeight = c(90.6, 90.5, 91.7))

dat %>% mutate(Date = str_extract(Date,the_time))
#>    Date JumpHeight BodyWeight
#> 1 18:18       35.1       90.6
#> 2 20:13       29.5       90.5
#> 3  9:15       36.2       91.7
1 Like

This is awesome thanks so much! Would you mind briefly explaining what the


 the_time <- "//d+://d{2}" 


code means so that I can do the same thing but keep the date information and remove the time information?

Thank you!

I got the problem backwards. the_time specifies the minute:second portion of the string representation of the time. To remove it, rather than keeping it, just use str_remove in place of str_extract.

1 Like

Thank you very much for your insight and assistance!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.