I'm trying to figure out how to use the same factor "mapping" for more than one column.
For example, here's a data set of imaginary tennis matches, in which one player was the favorite and the other player was the underdog:
df <- tribble(
~game, ~favorite, ~underdog,
#-----|----------|----------
1, "Mary", "Susan",
2, "Susan", "John",
3, "Mary", "Joe",
4, "Joe", "Dave",
5, "Susan", "Dave"
)
Note that there are favorites who are never underdogs (Mary) and underdogs who are never favorites (John, Dave).
I'd like to create a factor that works for both the favorite and underdog columns. For instance, if I now enter
df %<>% mutate(favorite=factor(favorite))
then Joe is 1, Mary is 2, and Susan is 3, since the default is alphabetical order.
How can I now change the underdog column to a factor so that every time Joe shows up there he is assigned 1, Mary gets 2, and so on?
My requirements/hopes/desires:
- In practice there will be a lot of rows and different entries, so hand coding is not an option;
- I'd like to be able to do this in the middle of a chain, with pipes;
- At the end, I'd like to be able to run code like
filter(favorite=="Susan" | underdog=="Susan")
.
I looked but couldn't find where this has been done before. Any help would be appreciated.