Mason Cary
Mason Cary

Reputation: 13

I'm trying to take out seconds in a value that shows HH:MM:SS

Using r in RStudio.

I have data that looks like this: 00:00:00 - 00:59:59 01:00:00 - 01:59:59 etc.

I want to transform the whole column of data to just show as: 0:00 - 0:59 01:00 - 1:59 etc.

I feel like the solutions is using str_replace somehow, but the regular expression stuff I've looked up doesn't seem to help with this particular solution. Any advice is appreciated, thanks!

Upvotes: 0

Views: 38

Answers (1)

Randall Helms
Randall Helms

Reputation: 859

you can do this with str_sub from the stringr package, like this

c <- tibble(v1 = c('00:00:00 - 00:59:59','01:00:00 - 01:59:59'))  %>%
#split your column into two separate columns
  separate(v1,into = c('v2','v3'),sep =' - ',remove = FALSE) %>%
#remove the seconds
  mutate_at(2:3, ~ str_sub(.,end=-4)) %>%
#paste it back together
  mutate(v4 = paste(v2,v3,sep=' - ')) %>%
#have a look at the original and fixed columns
  select(v1,v4)

Upvotes: 1

Related Questions