Suppose I have a dataframe:
> df <- data.frame(
id = 1:10,
name = c("Bob", "Ashley", "James", "David", "Jenny",
"Hans", "Leo", "John", "Emily", "Lee"),
gender = c("Male", "Female", "Male", "Male", "Female",
"Male", "Male", "Male", "Female", "Female"))
I don’t want the standard output for this:
> df
id name gender
1 1 Bob Male
2 2 Ashley Female
3 3 James Male
4 4 David Male
5 5 Jenny Female
6 6 Hans Male
7 7 Leo Male
8 8 John Male
9 9 Emily Female
10 10 Lee Female
Instead, I want to know which names go with female and which with male:
Female | Male |
---|---|
Ashley | Bob |
Jenny | James |
Emily | David |
Lee | Hans |
Leo | |
John |
There are plenty of functions that return the count of each (how many males, or how many James), but I’ve been unable to figure out how to just get the possible combinations.
[note that I’ve tried to update this post to have better formatting but it seems someone else is doing the same.]
This comes close
> split(df, ~gender) |> lapply(`[`, 'name')
$Female
name
2 Ashley
5 Jenny
9 Emily
10 Lee
$Male
name
1 Bob
3 James
4 David
6 Hans
7 Leo
8 John
maybe you want
> split(df$name, df$gender)
$Female
[1] "Ashley" "Jenny" "Emily" "Lee"
$Male
[1] "Bob" "James" "David" "Hans" "Leo" "John"
or
> split(df$name, df$gender) |> lapply(sort)
$Female
[1] "Ashley" "Emily" "Jenny" "Lee"
$Male
[1] "Bob" "David" "Hans" "James" "John" "Leo"