Home > OS >  Adding leading zeroes for multiple columns in dplyr (R)
Adding leading zeroes for multiple columns in dplyr (R)

Time:01-27

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("d", 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("d", 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.

CodePudding user response:

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("d", as.numeric(.x))))
#>     x   y
#> 1 001 004
#> 2 002 455
#> 3 032 006

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

CodePudding user response:

We may use str_pad from stringr - it can pad the character input with leading 0s

library(stringr)
library(dplyr)
X %>%
   mutate(across(all_of(lead_cols), str_pad, width = 3, pad = "0"))

-output

   x   y
1 001 004
2 002 455
3 032 006
  • Related