'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"
r


Solution 1:[1]

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])
}     

Solution 2:[2]

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")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nick Keramaris
Solution 2 Agile Bean