So let's say we have three lists.
list_A <- c("PA","MA","MD")
list_B <- c("NJ","NY","OK")
list_C <- c("AZ","MT","LA")
I have a dataframe like this
ID presenter state
1 Donatello c("AZ","NY")
2 Leonardo NJ
3 Rafael c("LA","MT")
4 Michaelangelo c("PA,"LA")
I want to use the tidyverse to create three new vars which count how many times state contains an element of their list for each row.
ID presenter state A B C
1 Donatello c("AZ","NY") 0 1 1
2 Leonardo NJ 0 1 0
3 Rafael c("LA","MT") 0 0 2
4 Michaelangelo c("PA","LA") 1 0 1
Unrelated but related, but out of curiousity, is it possible to unlist() the state but make more rows with the old info, so manipulating df1...
ID presenter state
1 Donatello AZ
1 Donatello NY
2 Leonardo NJ
3 Rafael LA
3 Rafael MT
4 Michaelangelo PA
4 Michaelangelo LA
Aucun commentaire:
Enregistrer un commentaire