How to transform the dataframe [duplicate]

Faced trouble using R trying to transform my dataset. The dataset is larger, with more food types, person IDs and "how often" options.

Please advice


This type of transformation is called a "pivot". We are "pivoting" the data set from a long (many rows) format to a wide (many columns) format. This is easily done using the tidyr pivot_wider.

library(tidyr)
df_transformed<-df %>%
   pivot_wider(names_from="food_type", values_from="how_often")