Al14
Al14

Reputation: 1814

replace a specific strings from multiple columns in a dataframe

I want to replace the strings "aa" and "aaa" in the dataframe below with""

data = data.frame(attr = c(1:4), type1=c('aa','b'), type2=c("aaa", "aa"))

data <- apply(data,2,function(x) gsub("aa",'',x))

gsub takes every aa and gsub(c("aa", "aaa"),'',x) does not work

Upvotes: 3

Views: 5399

Answers (4)

Leonardo Siqueira
Leonardo Siqueira

Reputation: 371

library(dplyr)
data2 = data %>%
  mutate(type1 = recode(type1, "aa" = "", "aaa" = ""),
         type2 = recode(type2, "aa" = "", "aaa" = ""))

attr type1 type2
1    1            
2    2     b      
3    3            
4    4     b      

if you wanna generalize, taking from @denis answer:

data2 = data %>%
  mutate_all(function(x) gsub("aa|aaa","",x))

Upvotes: 3

h3rm4n
h3rm4n

Reputation: 4187

 dat[2:3] <- lapply(dat[2:3], gsub, pattern = '[a]{2,}', replacement = '')

The result:

> dat
  attr type1 type2
1    1            
2    2     b      
3    3            
4    4     b      

Data used:

dat <- data.frame(attr = c(1:4), type1 = c('aa','b'), type2 = c("aaa", "aa"))

Upvotes: 3

jay.sf
jay.sf

Reputation: 72613

You could just set option fixed = FALSE and delete all the a's.

data <- as.data.frame(apply(data, 2, function(x) gsub("a",'', x, fixed = FALSE)))
data
#   attr type1 type2
# 1    1            
# 2    2     b      
# 3    3            
# 4    4     b  

Upvotes: 1

denis
denis

Reputation: 5673

data <- apply(data,2,function(x) gsub("aa|aaa","",x))
     attr type1 type2
[1,] "1"  ""    ""   
[2,] "2"  "b"   ""   
[3,] "3"  ""    ""   
[4,] "4"  "b"   ""   

You could alternatively use

data <- apply(data,2,function(x) gsub("a{2,3}","",x))

Upvotes: 4

Related Questions