I'm surprised to find no one asked this question on Stackoverflow before. Maybe it's too stupid to ask?
So I have a dataframe that contains 48 weather variables, each representing a weather value for a month. I have drawn a simplified table shown below:
weather 1 | weather 2 | weather 3 | weather 4 | weather 5 | weather 6 | weather 7 | weather 8 | weather 9 | weather 10 | weather 11 | weather 12 |
---|---|---|---|---|---|---|---|---|---|---|---|
12 | 6 | 34 | 9 | 100 | .01 | -4 | 38 | 64 | 77 | 21 | 34 |
99 | 42 | -3 | 34 | 34 | .5 | 27 | 19 | 7 | 18 | NA | 20 |
My objective is to make the column names from "weather 1, weather 2, ..." to "weather 01, weather 02, ...." And I wrote a loop like this:
for (i in 1:9){
colnames(df) = gsub(i, 0 i, colnames(df))
}
However, instead of replacing the single-digit numbers with a leading zero, R replaced the actual letter "i" with "0 i". Can anyone let me know what's going on here and how to fix it? Or is there a better way to add leading zeros to column names?
Thank you very much!
CodePudding user response:
We can use
library(stringr)
colnames(df) <- str_replace(colnames(df), "\\d ",
function(x) sprintf("d", as.integer(x)))
CodePudding user response:
Here is another option:
library(tidyverse)
set.seed(35)
example <- tibble(`weather 1` = runif(2),
`weather 2` = runif(2),
`weather 3` = runif(2))
rename_with(example, ~str_replace(., "(weather )(\\d )", "\\10\\2"), everything())
#> # A tibble: 2 x 3
#> `weather 01` `weather 02` `weather 03`
#> <dbl> <dbl> <dbl>
#> 1 0.857 0.553 0.486
#> 2 0.0108 0.950 0.0939
or with base R
colnames(example) <- gsub("(weather )(\\d )", "\\10\\2", colnames(example))
example
#> # A tibble: 2 x 3
#> `weather 01` `weather 02` `weather 03`
#> <dbl> <dbl> <dbl>
#> 1 0.857 0.553 0.486
#> 2 0.0108 0.950 0.0939