Str_replace_all - how to use


#1

I am practising some R skills on some dummy data. I want to replace all specific values in a very large data set with other values. So for example I want to replace ALL of the instances of "Long Hair" with a blank character cell as such " ". Sounds nuts but there is a point to it! I tried using the following...

df1 %>% str_replace("Long Hair", " ")

Can anyone advise how to correct - thank you.


#2

Hi Elle!

Welcome to community.rstudio.com! To help you get the right help for your question, can you please turn it into a reprex (reproducible example)? This will ensure we're all looking at the same data and code.

A guide for creating a reprex can be found here.


#3

This is the documentation:
https://stringr.tidyverse.org/reference/str_replace.html

You can see that the string must be a vector (or a column of a dataframe).


#4

Is it not working as is?

suppressPackageStartupMessages(library(tidyverse))
library(stringr)

hairdo <- c("Long Hair", "Short Hair", "Bob", "Weave", "Long Hair", "Longer Hair", "Another Bob", "Long Hair")

hairdid <- hairdo %>%
  str_replace_all("Long Hair", " ")

hairdid
#> [1] " "           "Short Hair"  "Bob"         "Weave"       " "          
#> [6] "Longer Hair" "Another Bob" " "

Created on 2018-08-21 by the reprex package (v0.2.0.9000).

Also see below for various data-frame-based implementations


#5

Hi kmprioli

As requested here is my code (sorry about that, lazy is the only comment I have)

library(tidyverse)
df1 <- tibble(
  hair1 = c("Long Hair", "Short Hair", "Medium Hair", "Medium Hair"),
  hair2 = c("Long Hair", "Long Hair", "Short Hair", "Medium Hair"),
  hair3 = c("Long Hair", "Short Hair", "Long Hair", "Medium Hair")
)

df2 <- df1 %>% str_replace("Long Hair", " ")

So as Martin said, it will work on a column but how to make it work on the whole data set? I also tried using str_replace_all but no joy :frowning:


#6

Welcome !

Could you try with mutate_all?

library(tidyverse)
df1 <- tibble(
  hair1 = c("Long Hair", "Short Hair", "Medium Hair", "Medium Hair"),
  hair2 = c("Long Hair", "Long Hair", "Short Hair", "Medium Hair"),
  hair3 = c("Long Hair", "Short Hair", "Long Hair", "Medium Hair")
)

df2 <- df1 %>% 
    mutate_all(funs(str_replace(., "Long Hair", " ")))

This is the way I address these type of problems, maybe it could help. However, I'm not sure if there is a better approach.


#7

Thank you 'iugax' - this worked - brilliant :grinning:


#8

You should probably choose @iugax's solution then :slight_smile: