R Data Cleaning Error

Lesson Link: https://app.dataquest.io/m/323/data-cleaning-with-r/7/class-size-data-creating-a-key-using-string-manipulation

My code:
csd <- str_pad(class_size$CSD, 2, side = ‘left’, pad = ‘0’)
DBN <- str_c(csd, class_size$SCHOOL CODE)

class_size <- class_size >
mutate(DBN = DBN)

Answer code:
class_size <- class_size >
mutate(DBN = str_c(CSD, SCHOOL CODE, sep = “”)) >
mutate(DBN = str_pad(DBN, width = 6, side = ‘left’, pad = “0”))

Error I’m receiving:

So i would like to know why I’m receiving this error and what the syntax issues might be.
I am trying to understand how to debug it without looking at the answer.

Thank you,

Hi @shankhap1,
As the error indicates, the length of DBN is 583 which corresponds to the number of rows in the class_size dataframe. mutate() is a vectorized function which will append values to the DBN column row by row. Since DBN holds 583 values it cannot append all 583 values for each row in the DBN column. That is why the answer code works. Here, the mutate() function applies str_c() on the CSD and SCHOOL CODE columns to get the corresponding values for the DBN column one row at a time.