I feel like this should be fairly simple, but I'm stuck. I have a list of isolates that start with the letter P + varying numbers. I would like to have each to have 4 numbers with leading 0s to fill in. For example, "P10" -> "P0010" and "P104" -> "P0104".
I extracted the number and was trying to glue it together by adding 0's, but I'm getting an error I don't understand. I think it might be due to the rep
function, but not super certain. Any help would be appreciated!
dat <- data.frame(isolate = c("P10", "P104", "P2"))
dat %>%
mutate(num = str_extract(isolate, "[:digit:]+")) %>%
mutate(final = str_glue("P", rep(0,4-length(num)), num))
The error message is as follows:
Error in `mutate()`:
ℹ In argument: `final = str_glue("P", rep(0, 4 - length(num)), num)`.
Caused by error:
! All unnamed arguments must be length 1