如何生成一个包含可能条目列表而不是计数的表格?

问题描述 投票:0回答:1

假设我有一个数据框:

> 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"))

我不想要这个的标准输出:

> 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

相反,我想知道哪些名字适合女性,哪些名字适合男性:

Female     Male
Ashley     Bob
Jenny      James
Emily      David
Lee        Hans
           Leo
           John

有很多函数可以返回每个的计数(有多少男性,或有多少詹姆斯),但我一直无法弄清楚如何获得可能的组合。

r datatables
1个回答
0
投票

这已经很接近了

> 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

也许你想要

> split(df$name, df$gender)
$Female
[1] "Ashley" "Jenny"  "Emily"  "Lee"   

$Male
[1] "Bob"   "James" "David" "Hans"  "Leo"   "John" 

> split(df$name, df$gender) |> lapply(sort)
$Female
[1] "Ashley" "Emily"  "Jenny"  "Lee"   

$Male
[1] "Bob"   "David" "Hans"  "James" "John"  "Leo"  
© www.soinside.com 2019 - 2024. All rights reserved.