Applying Gsub to Various Columns

Applying gsub to various columns

You can use apply to apply it to the whole data.frame

apply(x, 2, function(y) as.numeric(gsub("%", "", y)))
x1 x2 x3
[1,] 10 60 1
[2,] 20 50 2
[3,] 30 40 3

How can I use gsub in multiple specific column in r

We can use lapply to loop over the columns and apply the gsub

nm1 <- c("col1", "col3", "col5") 
data[nm1] <- lapply(data[nm1], gsub, pattern = "sfsdf", replacement = "Hi")

Or another option is mutate_at

library(dplyr)
data %>%
mutate_at(vars(nm1), ~ str_replace(., "sfsdf", "Hi"))

Apply and function a gsub in a lots of columns

Here is another solution. It returns all the columns of the original dataframe

library(dplyr)
mutate_at(x, 6:12, gsub("\\.", "", .))

apply multiple gsub functions columns of dataframe, R

Make your function working for one column -

fn <- function(x) {
x %>%
tolower() %>%
gsub("@\\w+https?://.+\\d+\\w*\\d*#\\w+[^\x01-\x7F]^\\s+\\s+$", "", .) %>%
gsub("[[:punct:]]", " ", .) %>%
gsub("[ |\t]+", " ", .) %>%
gsub("senior", "", .) %>%
gsub("junior", "", .) %>%
gsub("trainee", "", .) %>%
gsub("head", "", .)
}

Then you can apply it for every column using lapply -

jobs[] <- lapply(jobs, fn)

Or across in dplyr -

library(dplyr)
jobs %>% mutate(across(.fns = fn))

Easiest way to replace values in multiple columns at once in R

Use fixed = TRUE:

df[,1:2] = apply(df[,1:2], 2, function(x) gsub('*',NA,x,fixed = TRUE))

* is used as a regular expression otherwise.

gsub() on all values in a dataframe with multiple replacements

lapply returns a list you can assign it to dataframe with [] to keep the dimensions.

Land_Use[] <- lapply(Land_Use, function(y) gsub("native forest", "forest", y))

Here gsub will be applied to all the column in the dataframe.

For one column you need to assign the output back to column again instead of dataframe.

Land_Use$`1972` <- gsub('native forest','forest.',Land_Use$`1972`)

If you want to change multiple values into one value you may want to look at fct_collapse function from forcats.

library(dplyr)
library(forcats)

Land_Use %>%
mutate(across(.fns = ~fct_collapse(.x, 'Forest' = c('native forest', 'exotic forest'),
'water' = c('lake', 'river', 'ocean', 'pond')))) -> Land_Use

Land_Use

How to apply gsub or similar to change column names but only if column name contain specific word

You can use

colnames(a) <- sub(".*CSF-([^._]*).*", "\\1", colnames(a))

See the regex demo. Details:

  • .* - any zero or more chars as many as possible
  • CSF- - CSF- text
  • ([^._]*) - capturing group 1 (\1 refers to the group value from the replacement pattern): any zero or more chars other than . and _
  • .* - the rest of the string.

gsub across each row of multiple columns of dataframe R

Try the stringr package, and an edit suggested by Nicola -fixed(dat1$col2)

library(stringr)
str_replace(dat1$Col1, fixed(dat1$Col2), "")

"a woman's hat long" " company news" " house" "this would weigh "
>

apply gsub over a certain column in a list of data frames

Solution with tidyverse

library(purrr)
library(dplyr)
library(stringr)

map(results1, ~.x[]%>%
mutate(names = str_replace_all(names,"\\.\\.", "")))

[[1]]
names coefficients
1 a15.pdf 1.27679608
2 a17.pdf 1.05090176
3 a18.pdf 1.51820192
4 a21.pdf 2.30296037
5 a2TTT.pdf 1.48568732
6 a5.pdf 0.49371310
7 B11.pdf 1.02705905
8 B12.pdf 0.99974736
9 B13.pdf 2.40828102
10 B22.pdf 0.69515213


Related Topics



Leave a reply



Submit