BigDataScientist
BigDataScientist

Reputation: 1084

Remove quotes ("") from a data.frame in R

I have a data.frame with several columns, all of them are character class. All values are in double quotes, I would like to remove those quotes.

Example

df1      df2
"1203"   "Name1"
"2304"   "Name2"

Upvotes: 16

Views: 58207

Answers (3)

Agile Bean
Agile Bean

Reputation: 7181

Update dplyr 1.0.0

Since dplyr 1.0.0, you can use the new across syntax from purrr which makes it more readable for many of us.

df <- structure(list(Col1 = c("\"2515\"", "\"3348\"", "\"3370\""), Col2 = c("\"06/25/2013\"", "\"12/26/2013\"", "\"12/30/2013\"" )), .Names = c("Col1", "Col2"), row.names = c(NA, 3L), class = "data.frame") 

df
    Col1         Col2
1 "2515" "06/25/2013"
2 "3348" "12/26/2013"
3 "3370" "12/30/2013"

df %>% 
  mutate(across(
    everything(),
    ~ map_chr(.x, ~ gsub("\"", "", .x))
  ))

  Col1       Col2
1 2515 06/25/2013
2 3348 12/26/2013
3 3370 12/30/2013

The advantage of this across syntax is that it is not only nicely readable but also very flexible. Instead of everything() for all columns, you can use a range of other methods to reference columns, e.g.

  • by name (Col1, Col2)
  • by data type (e.g. is.numeric, is.character)
  • by other tidyselect selection helpers (e.g. starts_with("Col"), contains("Col")

Upvotes: 1

Nick Keramaris
Nick Keramaris

Reputation: 397

LukeA's answer converted my entire dataframe to characters, so I implemented this modification, which only modifies the columns that are character class:

character_cols = which(sapply(x, class) == 'character')

for(i in 1:length(character_cols)) {
  a = character_cols[i]
  x[,a] = gsub("\"", "", x[,a])
}     

Upvotes: 0

lukeA
lukeA

Reputation: 54277

The print() method for data frames has an option quote=, which you can set to FALSE:

print.data.frame(data.frame(x=c("Hello", "World")), 
                 quote=FALSE)
#       x
# 1 Hello
# 2 World

See also ?print.data.frame(= help)

Edit:

With regards to the dputed data in the comment below:

as.data.frame(sapply(df, function(x) gsub("\"", "", x)))

Upvotes: 21

Related Questions