rdplyrleading-zero

Adding leading zeroes for multiple columns in dplyr (R)


I'm looking to add zeroes to several columns at the same time in R using dplyr syntax. (I've come across some similar questions, but none gives me exactly what I want; they're either for single columns or for base syntax.)

I have this:

    x   y
1 001  04
2   2 455
3  32   6

and I want this:

    x   y
1 001 004
2 002 455
3 032 006

Here's a solution that I found, but not with dplyr and piping

# Make data
x <- c("001", "2", "32")
y <- c("04", "455", "6")
X <- data.frame(cbind(x,y))

# Define and apply function with lapply
lead_cols <- c("x","y")

lead_zeros <- function(col) {
  sprintf("%03d", as.numeric(col))
  }
X[lead_cols] <- lapply(X[lead_cols], lead_zeros)

Here's what I've come up with in dplyr, but it doesn't work:

X[lead_cols] <- X %>% mutate_at(lead_cols, sprintf("%03d", as.numeric(lead_cols)))

I get this error:

Error in get(.x, .env, mode = "function") : 

object ' NA' of mode 'function' was not found

The solution must be simple, but I've been banging my head against the wall and just want some help, please.


Solution

  • Try with a formula or anonymous function in that mutate call. BTW, mutate_at() is superseded by across().

    library(dplyr)
    x <- c("001", "2", "32")
    y <- c("04", "455", "6")
    X <- data.frame(cbind(x,y))
    
    lead_cols <- c("x","y")
    
    X %>% mutate(across(all_of(lead_cols), ~ sprintf("%03d", as.numeric(.x))))
    #>     x   y
    #> 1 001 004
    #> 2 002 455
    #> 3 032 006
    

    Created on 2023-01-27 with reprex v2.0.2