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"
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 dput
ed data in the comment below:
as.data.frame(sapply(df, function(x) gsub("\"", "", x)))
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, eg
Col1, Col2
)is.numeric
, is.character
)starts_with("Col")
, contains("Col"
)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])
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.