I everyone,
I am currently learning R and trying to tidy up a dataset using pivot_longer() from the tidyverse package.
I have this tibble
title actor_1 actor_2 actor_3 actor_1_FB_likes actor_2_FB_likes actor_3_FB_likes
<chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
1 Avatar CCH Pound… Joel Davi… Wes Studi 1000 936 855
2 Pirates of the Car… Johnny De… Orlando B… Jack Daven… 40000 5000 1000
3 The Dark Knight Ri… Tom Hardy Christian… Joseph Gor… 27000 23000 23000
4 John Carter Daryl Sab… Samantha … Polly Walk… 640 632 530
5 Spider-Man 3 J.K. Simm… James Fra… Kirsten Du… 24000 11000 4000
6 Tangled Brad Garr… Donna Mur… M.C. Gainey 799 553 284
I want to change it into the following format:
title actor_name num_likes
<chr> <chr> <dbl>
1 Avatar CCH Pounder 1000
2 Avatar Joel David Moore 936
2 Avatar Wes Studi 855
and so on... unfortunately I am stuck. No matter what I try I somehow end up with something like this format:
title actor_num actor_name actor_likes num_likes
<chr> <chr> <chr> <chr> <dbl>
1 Avatar actor_1 CCH Pounder actor_1_FB_likes 1000
2 Avatar actor_1 CCH Pounder actor_2_FB_likes 936
3 Avatar actor_1 CCH Pounder actor_3_FB_likes 855
4 Avatar actor_2 Joel David Moore actor_1_FB_likes 1000
5 Avatar actor_2 Joel David Moore actor_2_FB_likes 936
6 Avatar actor_2 Joel David Moore actor_3_FB_likes 855
7 Avatar actor_3 Wes Studi actor_1_FB_likes 1000
8 Avatar actor_3 Wes Studi actor_2_FB_likes 936
9 Avatar actor_3 Wes Studi actor_3_FB_likes 855
My last attempt consisted of the following steps:
exercise8 <- exercise8 %>% pivot_longer(cols= actor_1:actor_3, names_to='actor_num', values_to='actor_name')
exercise8 <- exercise8 %>% pivot_longer(cols= actor_1_FB_likes:actor_3_FB_likes, names_to='actor_likes', values_to='num_likes')
I can of course delete the columns actor_num and actor_likes but that would still not result in the desired format.
Can anyone help? Am I starting completely wrong or is there a last step I am missing? Thank you in advance!