I have the following df:
names sex
adam M
jill F
stewart M
jordan M
alica F
jordan F
How do I filter the rows so that I only get the names that are both M and F, in this case, jordan.
I have the following df:
names sex
adam M
jill F
stewart M
jordan M
alica F
jordan F
How do I filter the rows so that I only get the names that are both M and F, in this case, jordan.
If all your data is like this, you can simply find rows with duplicate values:
Example:
or if you want a vector of names:
We can group by 'names' and
filter
the 'sex' havingunique
number of elements greater than 1Or another option is to group by 'names' and
filter
the groups having both the 'M' and 'F'