user213544
user213544

Reputation: 2126

Rename row identifiers in R

I have a dataframe in R that look like this:

data.frame(ID = seq(1, 12, 1), 
           value = rnorm(12))

#>    ID       value
#> 1   1 -0.05695300
#> 2   2 -0.95685356
#> 3   3  0.32508199
#> 4   4 -1.26247870
#> 5   5  0.65572362
#> 6   6 -1.23285703
#> 7   7 -1.57634388
#> 8   8 -0.50605901
#> 9   9 -0.52063312
#> 10 10  0.76800781
#> 11 11  1.10101402
#> 12 12  0.09528496

I would like to rename the IDs to ID-01, ID-02 ... ID-12.

It feels like a dplyr mutate-job, however, I am not sure how to do it.

How would you do this is R?

Upvotes: 0

Views: 150

Answers (1)

Roland
Roland

Reputation: 132969

I don't see how this is a job for dplyr (but I don't use that package). It's easy with base R:

DF$ID <- sprintf("ID-%02d", DF$ID)

%02d means integer with two digits padded with zero in front, see help("sprintf").

Upvotes: 5

Related Questions