Fundamentals of String Manipulation

Instructions

  • Use str_pad() to bring the length of all days of the match_day variable you created to two by adding a zero to the left side of the strings.
  • Add the match_day variable to the scores data frame. Overwrite the original scores data frame with the one containing match_day .
  • Use str_length to check that the length of all strings in the match_day vector is two characters. Save the resulting vector as match_day_length .

match_matrix <- str_split(scores$match_date, " ", simplify = TRUE)
match_day <- match_matrix[,1]

scores <- scores > mutate(match_day = str_pad(match_day,2, side = “left”, pad = “0”))

match_day_length <- str_length(scores$match_date)

I am still lost on this topic and need help understanding what was done inside mutate().

Hello @amannegassi,

In the future, it would be helpful to provide also the number of the screen you have questions about so it’s easier for us to retrieve it.

  • The str_pad() function is used to pad a string to an input string/vector of strings of length less than a given length. In the following piece of code str_pad(string = match_day, width = 2, side = "left", pad = "0"), we pad a “0” on the left side of the string of length less than 2.

  • The mutate() function is used to either create a new variable or overwrite an existing one.

mutate( the_name_of_the_variable_to_create/overwrite = the_values_we_want_to_place_in_this_variable)

Hence, in the following piece of code mutate(match_day = str_pad(match_day,2, side = "left", pad = "0")), we overwrite the existing match_day variable with the output of the str_pad() function i.e. the vector of strings representing match days where “0” is padded on the left side of each string of length less than 2.

Best,
John.