I'm trying to format my base_df
to show users_id
organized by program watched.
base_df:
base_df <- structure(list(category = c("News", "News", "Sports", "Sports",
"sports", "Sports", "Sports", "sports"), programs = c("News A",
"News B", "Sports A", "Sports B", "sports C", "Sports A", "Sports B",
"sports C"), users_id = c(10003831, 10003823, 10003841, 10003823,
10003851, 10003851, 10003851, 10003854)), row.names = c(NA, -8L
), class = c("tbl_df", "tbl", "data.frame"))
Desired output:
I think pivot_longer
can help me here:
I've tried to use it but it returns a df with just the users_ids. What am I doing wrong?
b <- pivot_wider(
base_df,
id_cols = users_id,
names_from = programs
)