'Remove multiple underscores in a column name in R

Here's a sample of my data

structure(list(uianame = c("1% for_the_Planet", "1% for_the_Planet", 
"1% for_the_Planet", "1% for_the_Planet", "1% for_the_Planet", 
"1% for_the_Planet", "1% for_the_Planet", "1% for_the_Planet", 
"1% for_the_Planet", "1% for_the_Planet", "1% for_the_Planet", 
"1% for_the_Planet", "1% for_the_Planet", "1% for_the_Planet", 
"1% for_the_Planet", "1% for_the_Planet", "1% for_the_Planet", 
"350.org", "350.org", "350.org", "350.org", "350.org", "350.org", 
"350.org", "350.org", "350.org", "350.org", "350.org", "350.org", 
"350.org", "4C Association", "4C Association", "4C Association", 
"4C Association", "4C Association", "4C Association", "5 Gyres_Institute", 
"5 Gyres_Institute", "5 Gyres_Institute", "5 Gyres_Institute", 
"5 Gyres_Institute", "5 Gyres_Institute", "5 Gyres_Institute", 
"5 Gyres_Institute", "5 Gyres_Institute", "5 Gyres_Institute", 
"A Rocha_International", "A Rocha_International", "A Rocha_International", 
"A Rocha_International", "A Rocha_International", "A Rocha_International", 
"A Rocha_International", "A Rocha_International", "A Rocha_International", 
"A Rocha_International", "Aalborg Centre_for_Problem_Based_Learning_in_Engineering_Science_and_Sustainability", 
"Aalborg Centre_for_Problem_Based_Learning_in_Engineering_Science_and_Sustainability", 
"Action for_a_Global_Climate_Community")), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -59L), groups = structure(list(
    uianame = c("1% for_the_Planet", "350.org", "4C Association", 
    "5 Gyres_Institute", "A Rocha_International", "Aalborg Centre_for_Problem_Based_Learning_in_Engineering_Science_and_Sustainability", 
    "Action for_a_Global_Climate_Community"), .rows = structure(list(
        1:17, 18:30, 31:36, 37:46, 47:56, 57:58, 59L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -7L), .drop = TRUE))

Here's what I've tried to do in a "tidy" workflow:

sample<- sample %>% mutate(uianame = str_replace(uianame, "_", " ")) ## space instead of "_"

It works -- but only for the first underscore. Any suggestions as how to fix this? Thanks.

NOTE: there are previous related questions, but none of them deal with this particular problem.



Solution 1:[1]

The answer is, as suggest in the comments by Maƫl: str_replace_all

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ferenc