0

I was just wondering if there was a way to delete the apostrophes in all of the cells in a data frame in R. I have tried adding using lapply, and apply to achieve this but nothing has worked so far. Thanks for your help!

Christopher Yee
  • 535
  • 2
  • 5
  • 14
  • 1
    Please read [ask] and [how to give a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). As your question now stands it is way too broad. Try to be as specific as possible. – Jaap Mar 09 '16 at 20:27

4 Answers4

0

This should work:

for(i in 1:ncol(df)){
  df[,i] <- gsub("'","",df[,i])
}
Hack-R
  • 22,422
  • 14
  • 75
  • 131
0

You could also try this:

df2 <-  as.data.frame(sapply(df, function(x) gsub("'", "", x)))
Wyldsoul
  • 1,468
  • 10
  • 16
0

Pattern matching and replacement is easily done with the grep functions: https://stat.ethz.ch/R-manual/R-devel/library/base/html/grep.html

A simple example:

z <- c("A'", "B", "C'")
x <- c("D", "E", "F'")
D <- data.frame(z, x)
D$z <- gsub("'", "", D$z)
D$x <- gsub("'", "", D$x)

This post provides another implementation example: Replacing data values based on grep result in R

Community
  • 1
  • 1
Dominix
  • 433
  • 3
  • 9
0

My data frame below:

 my_df=data.frame(a=c("a'a","b"),b=c("c","d'd"))
 my_df
 #     a   b
 # 1 a'a   c
 # 2 b   d'd

 result=as.data.frame(lapply(my_df,gsub,pattern="'",replacement=""))
 result
 #   a   b
 # 1 aa  c
 # 2 b  dd

Hope this helped. Thank you.

Sowmya S. Manian
  • 3,723
  • 3
  • 18
  • 30