Deleting specific letters from a character variable


I am working with URL addresses as values of a variable.

Some of the addresses have a c in front of them. For example my data look something like this:


How do I get rid of that first c without also removing the c in .com?

For example, I was trying this code but it was removing both c's

str_remove(URL$ext_media_url, "[c]")

I also can't remove just the first character because it would remove the "h" from the correct http urls.

Any advice would be greatly appreciated!


I think this works

mytext  <-  c("c", "", 
              "c", "")

mytext<-sub(".", "", mytext)

mytext <- c("c", "",
"c", "")

mytext <- gsub("chttp", "http", mytext)


Assuming you don't mind using the tidyverse library, this should do what you're looking for:


urls  <-  c("c", 

## remove character c only at the start of the string
urls_cleaned <- urls %>% str_remove("^c")

tibble(urls, urls_cleaned)
#> # A tibble: 4 x 2
#>   urls                 urls_cleaned       
#>   <chr>                <chr>              
#> 1 c 
#> 2
#> 3 c
#> 4

Created on 2021-01-29 by the reprex package (v1.0.0)

This topic was automatically closed 21 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.