Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove a character from the entire data frame

Tags:

dataframe

r

I have a dataframe with various columns, Some of the data within some columns contain double quotes, I want to remove these, for eg:

ID    name   value1     value2
"1     x     a,"b,"c     x"
"2     y     d,"r"       z"

I want this to look like this:

ID    name   value1    value2
1     x      a,b,c      x
2     y      d,r        z
like image 826
Anubhav Dikshit Avatar asked Sep 30 '15 04:09

Anubhav Dikshit


People also ask

How do I strip a whole data frame?

You can use DataFrame. select_dtypes to select string columns and then apply function str. strip .

How do I remove a specific character from a string in R?

You can either use R base function gsub() or use str_replace() from stringr package to remove characters from a string or text. In this article, I will explain how to remove a single character or multiple characters from a String in R by using gsub() and str_replace() functions.


2 Answers

I would use lapply to loop over the columns and then replace the " using gsub.

df1[] <- lapply(df1, gsub, pattern='"', replacement='')
df1
#  ID name value1 value2
#1  1    x  a,b,c      x
#2  2    y    d,r      z

and if need the class can be changed with type.convert

df1[] <- lapply(df1, type.convert)

data

df1 <-  structure(list(ID = c("\"1", "\"2"), name = c("x", "y"),
value1 = c("a,\"b,\"c", 
"d,\"r\""), value2 = c("x\"", "z\"")), .Names = c("ID", "name", 
"value1", "value2"), class = "data.frame", row.names = c(NA, -2L))
like image 81
akrun Avatar answered Sep 20 '22 19:09

akrun


One option would be to use apply() along with the gsub() function to remove all double quotation marks:

df <- data.frame(ID=c("\"1", "\"2"),
                 name=c("x", "y"),
                 value1=c("a,\"b,\"c", "d,\"r\""),
                 value2=c("x\"", "z\""))

df <- data.frame(apply(df, 2, function(x) {
                                  x <- gsub("\"", "", x)
                              })

> df
  ID name value1 value2
1  1    x  a,b,c      x
2  2    y    d,r      z
like image 41
Tim Biegeleisen Avatar answered Sep 22 '22 19:09

Tim Biegeleisen