NewBee
NewBee

Reputation: 1040

Use gsub to replace string within specific columns

I have data such as this:

data_in <- read_table2("V1  v2  V3  V4  V5  V6  U1
3   8   30  60  9   30  ertr
0   0   0   50  9   50  rt
10  15  60min   50% 8   45  yt
0           5   32  250 yt
0   0   0   5   36  225 ertr
0           33  20  120 rt
100%    12  100 30  15  50  yt
0   0   0   25  18  25  yt
0           1   2   45  ertr
1   2   45% 1   36  30  min
            1   36  50  yt
0           1   10  45  yt
            1   36  60  ertr
0   0   0   100 16  100 rt
")

I want to replace the "%" and "min" with a blank space, for the columns V1:V6.

I have code such as this, but it's not doing what I want. I guess I don't quite understand how to execute the new "across" feature.

data_in %>% mutate(across(starts_with("V"),~gsub("%|min","")))

Any suggestions appreciated!

Upvotes: 2

Views: 1268

Answers (2)

Duck
Duck

Reputation: 39595

You could also try:

data_in <- as.data.frame(apply(data_in, 2, function(x) gsub('%'," ",x)))
data_in <- as.data.frame(apply(data_in, 2, function(x) gsub('min'," ",x)))

     V1 v2  V3   V4   V5   V6   U1
1     3  8  30   60    9   30 ertr
2     0  0   0   50    9   50   rt
3    10 15 60   50     8   45   yt
4     0  5  32  250   yt <NA> <NA>
5     0  0   0    5   36  225 ertr
6     0 33  20  120   rt <NA> <NA>
7  100  12 100   30   15   50   yt
8     0  0   0   25   18   25   yt
9     0  1   2   45 ertr <NA> <NA>
10    1  2 45     1   36   30     
11    1 36  50   yt <NA> <NA> <NA>
12    0  1  10   45   yt <NA> <NA>
13    1 36  60 ertr <NA> <NA> <NA>
14    0  0   0  100   16  100   rt

Upvotes: -1

akrun
akrun

Reputation: 887028

In the gsub command, we need the x which would be

x - a character vector where matches are sought, or an object which can be coerced by as.character to a character vector.

as the usage is

gsub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE)

Within the across or mutate_at, there is anonymous function (function(x) or ~ and if we use the latter, the 'x' would be . or .x)

library(dplyr)
data_in2 <- data_in %>%
          mutate(across(starts_with("V"),~ gsub("%|min","", .)))

Or without anonymous functionm we can specify the arguments

data_in %>%
       mutate(across(starts_with("V"), gsub, pattern = "%|min", replacement = ""))

Or use str_remove

library(stringr)
data_in %>%
      mutate(across(starts_with("V"), str_remove_all, pattern = "%|min"))

Upvotes: 2

Related Questions