String_Padding : 496-4

Screen Link:

This is the mission in DQ:
The recent_grads dataset has been loaded in. It turns out that the major_code column is formatted incorrectly and actually has leading zeroes. For example, the number “2419” should actually be “0002419”.

DQ’s example to follow is:
str_pad(“Dataquest”, width = 20, side = “both”, pad = " ")
" Dataquest "

DQ’s answer to mission :

recent_grads ← recent_grads >
mutate(
formatted_major_code = str_pad(major_code, 7, pad = “0”)
)

My query:

Why does DQ’s answer doesn’t include ‘left’ , as the padding is to be done on left of the numbers in the column ‘major_code’

I don’t know R enough to know if there’s some well-known detail about it that makes it obvious or not, but, right now it just seems the documentation is lacking on this.

As per the source code on Github -

str_pad(x, 10) # default pads on left

However, in that same link, the function is defined as -

str_pad <- function(string, width, side = c("left", "right", "both"), pad = " ", use_width = TRUE) {
  vctrs::vec_size_common(string = string, width = width, pad = pad)
  side <- arg_match(side)

They call the function arg_match in the code.

As per Tidyverse design guide

To use this technique, set the default value to a character vector, where the first value is the default. Inside the function, use match.arg() or rlang::arg_match() which checks that the value comes from the known good set.

So, left is supposed to be the default value. The documentation doesn’t reflect that maybe because it’s meant to be common/basic knowledge that the first value is the default one in such cases in R, or maybe the documentation needs to be updated.

Either way, I would recommend providing feedback to DQ about this (assuming this wasn’t covered in the content at any point before this).