I have a dataset of movies with several columns listing actors/actresses appearing in the movie. The data is messy and sometimes the first column contains a missing value but the second contains an actor's name. I want to keep all the actor columns but move each non-missing value to the earliest column. For example:
movies <- data.frame(actor1=c("A","B",NA,"C",NA), actor2=c(NA, "Z", "W", NA, "X"), actor3=c("L","M","N","O","P"))
actor1 actor2 actor3
1 A <NA> L
2 B Z M
3 <NA> W N
4 C <NA> O
5 <NA> X P
Should become:
actor1 actor2 actor3
1 A L <NA>
2 B Z M
3 W N <NA>
4 C O <NA>
5 X P <NA>
coalesce() will pull W and X to the first column. Perfect. But how do I do the same for subsequent columns? For example, since W was pulled from actor2 to actor1, I now want the third row of actor2 to have the value N, not W.