If we wanted to filter data for a chosen cat, Apricot say, weโd also be filtering out rows of data belonging to Hansi. Also, there is no variable in the data that I can use to reference a specific cat; this information is in the variable names. As such, this data set is not tidy.
We can make the data tidy by pivoting the data set. We want to go from this wide representation of the data to a longer representation โ weโre effectively going to stack the two columns of weight data into a single column and create a new variable that identifies which row belongs to which of my cats. We do this with pivot_longer():
cat_long <- cat_weights |>pivot_longer(cols =everything(), # which variables are we pivotting?names_to ="cat", # name of variable to contain each cat's namevalues_to ="weight_kg"# name of the variable to hold the weights )