Reputation: 59
I have a dataframe of the following form
"column1" | "column2" |
---|---|
1 | 5 |
2 | 6 |
3 | 7 |
How do I remove the quotation mark from the column names? I've tried using gsub but I can't quote quotation marks haha. Also need a way to do this that isn't just names(data) <- c("column1", "column2")
. Thank you all!
Upvotes: 2
Views: 1750
Reputation: 93871
You can use gsub
with single-quotes in order to reference the double-quote character for replacement:
names(df) = gsub('"', "", names(df))
Test:
# Set up data
d = mtcars[1:3, 1:4]
names(d)[1:2] = c('"column1"', '"column2"')
names(d)
#> [1] "\"column1\"" "\"column2\"" "disp" "hp"
d
#> "column1" "column2" disp hp
#> Mazda RX4 21.0 6 160 110
#> Mazda RX4 Wag 21.0 6 160 110
#> Datsun 710 22.8 4 108 93
# Remove quotation marks from column names
names(d) = gsub('"', "", names(d))
names(d)
#> [1] "column1" "column2" "disp" "hp"
d
#> column1 column2 disp hp
#> Mazda RX4 21.0 6 160 110
#> Mazda RX4 Wag 21.0 6 160 110
#> Datsun 710 22.8 4 108 93
Created on 2021-01-19 by the reprex package (v0.3.0)
Upvotes: 2