I have a dataframe that effectively looks like this:
df_1 <- tribble(
~Name, ~activity1, ~number_activity_1, ~attendees1, ~activity2, ~number_activity_2, ~attendees2
"John", "Birthday", 1, 14, "Sleep Over", 4, 10,
"Chris", "Sleep Over", 2, 18, "Painting", 5, 8,
"Alex", "Track Race", 4, 100, "Birthday", 1, 5
)
I need to pivot_longer() while handling the groupings within my columns:
There are multiple activity values (in this case 1 and 2) There are 2 numbers (number_of_activity and attendees) for each activity value. In my actual dataset, there are 10 activities per person.
Essentially, what I'd like to do is to apply pivot_longer() to the all the variables that make up activity 1 and all the variables that make up activity 2
What I'd like to end up with is this:
df_2 <- tribble(
~Name, ~activity, ~number_activity, ~attendees,
"John", "Birthday", 1, 14,
"John", "Sleep Over", 4, 10,
"Chris", "Sleep Over", 2, 18,
"Chris", "Painting", 5, 8,
"Alex", "Track Race", 4, 100,
"Alex", "Birthday", 1, 5)
I have tried a few ways but I can't get the numbers to pivot with the matching activity.
Anyone know how to handle this?
Thank you!