Unfortunately I have a problem and would have to ask for your support.
I have a list of the most popular pet names. From the dataframe x, it appears that "Jerry" is the most popular mouse name, "Garfield" is the second most popular cat name, and so on.
x <- data.frame(animals = c("Mice", "Mice", "Mice", "Cats", "Cats", "Cats", "Dogs", "Dogs", "Dogs" ,"Birds", "Birds", "Birds"),
names = c("Jerry", "Speedy", "Feivel", "Tom", "Garfield", "Pumpkin", "Snoopy", "Pluto", "Sirius", "Tweety", "Donald", "Hedwig"),
rank = rep(c(1,2,3),4))
Dataframe y also contains pets and their names:
y <- data.frame(animals = c("Mice", "Cats", "Cats", "Dogs", "Birds"),
names = c("Pam", "Michael", "Garfield", "Sirius", "Tweety"))
I would now like to find out if the names of the animals in dataframe y are among the most popular animal names and if so, what the rank of that name would be. Thus, three columns should be created for each of the three ranks. If there is then a match with the most popular pet names a TRUE should be entered in the column with the corresponding rank. For example, "Pam" is not one of the most popular mouse names, so FALSE is entered in each of the three columns (or NA, I wouldn't care about that). On the other hand, Garfield is one of the most popular cat names, so the value TRUE should be entered in the rank2 column. This result should be stored in dataframe z.
z <- data.frame(animals = c("Mice", "Cats", "Cats", "Dogs", "Birds"),
names = c("Pam", "Michael", "Garfield", "Sirius", "Tweety"),
rank1 = c(FALSE, FALSE, FALSE, FALSE, TRUE),
rank2 = c(FALSE, FALSE, TRUE, FALSE, FALSE),
rank3 = c(FALSE, FALSE, FALSE, TRUE, FALSE))
I already thought about using the functions spread() or pivot_wider() from the tidyr package, but unfortunately I can't get the right result.
I would be very happy about your help and thank you very much in advance!