Here's a simplified Minimal Reprex of my problem. I have a data frame of people's name and their favorite color:
mydat <- data.frame("name"=c("bob","bob","alice","beth","patty","patty","patty"),
"fav_color"=c("green","blue","red","orange","green","red","pink"))
name fav_color
1 bob green
2 bob blue
3 alice red
4 beth orange
5 patty green
6 patty red
7 patty pink
Some people have more than one favorite color.
- How can I return this data frame if I only allow some max number of colors per name? like if I set some variable
maxcolors <- 2
how could I return this dataframe where each name only has 2 colors? I'd want:
name fav_color
1 bob green
2 bob blue
3 alice red
4 beth orange
5 patty green
6 patty red
- Is it possible to add an additional layer to question #1 by trying to allow colors that are unique to the total color list where possible?
Example: Ifmaxcolors <- 2
then Patty would only return 2 colors, but instead of 'green' and 'red -- which are both already present in the fav_colors column -- return green and pink, because at least 'pink' is not already present:
name fav_color
1 bob green
2 bob blue
3 alice red
4 beth orange
5 patty green
6 patty pink
EDIT : Should I be using slice_head
?