Use of Apply function for dataframe

My data look like this:

          word1                  word2        nprefix

 storms_come_in_here           is      4
     come_in_here_is      nothing      4
  in_here_is_nothing         like         4

here_is_nothing_like a 4
is_nothing_like_a southern 4
nothing_like_a_southern thunderstorm 4

I am trying to remove the underscores from the columns of word1 and replace them with blanks.
So for example “storms_come_in_here” would be “storms come in here”

The function to do that would be

library(stringr)
string_replace( x$word1, “_”, “ “)

To have my data look the same way as before, just changing the word1 column, ought I to use an apply() function? How to do that?

Try the mutate) function from the dplyr package.

library(stringr)
library(dplyr)
df <- data.frame(word1 = c("storms_com_in_here", "come_in_here_is", "in_here_is_nothing"),
                 word2 = c("is", "nothing", "like"), nprefix = c(4,4,4))
df <- df %>% mutate(word1 = str_replace_all(word1, "_", " "))
df
#>                word1   word2 nprefix
#> 1 storms com in here      is       4
#> 2    come in here is nothing       4
#> 3 in here is nothing    like       4

Created on 2019-07-19 by the reprex package (v0.2.1)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.