How to remove certain characters from a string?

Hello everybody,

I have a table with a variable which contains dates in the pattern YYYY-MM-DD.
I am looking for a way just to remove/delete the "-MM-DD" in this column.

Google gave me pages which told me to use str_sub() but this just won't work out as I want it to. It will only generate values which would only contain the years then and nothing else. I want to keep the other columns from the data frame, but str_sub() just won't let me.

What do? :confused:

library(stringr)
a_string <- "2020-08-12"
str_extract(a_string,"\\d{4}")
#> [1] "2020"

Created on 2020-09-12 by the reprex package (v0.3.0)

library(tidyverse)
toy_data <- data.frame(
  cabinet_id = c(996, 997),
  start_date = c("1901-03-30", "1903-09-24"),
  cabinet_name = c("Bartan", "Deakin1")
)
toy_data
#>   cabinet_id start_date cabinet_name
#> 1        996 1901-03-30       Bartan
#> 2        997 1903-09-24      Deakin1

toy_data %>% 
  separate(start_date, c("desired_variable_name", NA, NA))
#>   cabinet_id desired_variable_name cabinet_name
#> 1        996                  1901       Bartan
#> 2        997                  1903      Deakin1

Created on 2020-09-12 by the reprex package (v0.3.0)

Thanks, guys!

I found a simpler way which worked out in my case though:

parlgov_sel_fil$start_date <- substr(parlgov_sel_fil$start_date, 1,4)

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.