Deleting specific letters from a character variable

Hi,

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:

chttp://xyz.com.jpg
http://xyz2.com.jpg
chttp://xyz3.com.jpg
http://xyz4.com.jpg

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!

Thanks!

I think this works


mytext  <-  c("chttp://xyz.com.jpg", "http://xyz2.com.jpg", 
              "chttp://xyz3.com.jpg", "http://xyz4.com.jpg")

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

mytext <- c("chttp://xyz.com.jpg", "http://xyz2.com.jpg",
"chttp://xyz3.com.jpg", "http://xyz4.com.jpg")

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

mytext

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

library(tidyverse)

urls  <-  c("chttp://xyz.com.jpg", 
            "http://xyz2.com.jpg",
            "chttp://xyz3.com.jpg",
            "http://xyz4.com.jpg"
            )

## 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 chttp://xyz.com.jpg  http://xyz.com.jpg 
#> 2 http://xyz2.com.jpg  http://xyz2.com.jpg
#> 3 chttp://xyz3.com.jpg http://xyz3.com.jpg
#> 4 http://xyz4.com.jpg  http://xyz4.com.jpg

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.