'Trouble with string-replacing in a subset of R columns
I've tried to read through the archives to find a solution but no luck. I want to replace a string ("_" with " ") in a subset of columns in R:
January_2019= c(4, 5)
February_2019= c(1,2)
OtherVariable = c(3,5)
dateColumns <- c("January_2019",
"February_2019")
df <- data.frame(January_2019, February_2019, OtherVariable)
colnames(df[dateColumns]) <- gsub("_", " ", colnames(df[dateColumns]))
I cannot figure out why this is not working. What can I try next?
Solution 1:[1]
The simplest solution is really this:
names(df) <- gsub("_", " ", names(df))
Result:
df
January 2019 February 2019 OtherVariable
1 4 1 3
2 5 2 5
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 | Chris Ruehlemann |
