gpt4 book ai didi

r - 数据框到嵌套列表

转载 作者:行者123 更新时间:2023-12-01 07:51:53 27 4
gpt4 key购买 nike

我有一个从 .csv 文件中读取的数据框,如下所示:

              job    name `phone number`
<chr> <chr> <int>
1 developer john 654
2 developer mike 321
3 developer albert 987
4 manager dana 741
5 manager guy 852
6 manager anna 936
7 developer dan 951
8 developer shean 841
9 administrative rebeca 357
10 administrative krissy 984
11 administrative hilma 651
12 administrative otis 325
13 administrative piper 654
14 manager mendy 984
15 manager corliss 321

DT = structure(list(job = c("developer", "developer", "developer",
"manager", "manager", "manager", "developer", "developer", "administrative",
"administrative", "administrative", "administrative", "administrative",
"manager", "manager"), name = c("john", "mike", "albert", "dana",
"guy", "anna", "dan", "shean", "rebeca", "krissy", "hilma", "otis",
"piper", "mendy", "corliss"), phone = c(654L, 321L, 987L, 741L,
852L, 936L, 951L, 841L, 357L, 984L, 651L, 325L, 654L, 984L, 321L
)), .Names = c("job", "name", "phone"), row.names = c(NA, -15L
), class = "data.frame")

我想将其转换为列表列表,例如:
myList$developer

会给我一个包含所有开发人员的列表,然后
myList$developer$john

会给我一个与名为 John 的开发人员相关联的电话号码列表。有什么简单的方法吗?

如果您对我为什么想做这样的事情感到好奇:我正在使用的实际数据框很大,因此可以通过 4 个参数找到特定条目(在本例中,我可以找到具有 2 个参数的特定条目)参数:工作,名称)使用过滤器花费太多时间。我认为嵌套列表的哈希表结构可能需要很多时间来构建,但可以在 O(1) 中进行搜索,这对我来说绝对有用。
如果我错了,而您有更好的方法,我也很想听听。

最佳答案

您可以使用带有 splitlapply 参数的双 drop = TRUE 。使用 drop = TRUE 将删除不会发生的级别,从而防止创建空列表元素。

使用:

l <- split(dat, dat$job, drop = TRUE)
nestedlist <- lapply(l, function(x) split(x, x[['name']], drop = TRUE))

或者一口气:
nestedlist <- lapply(split(dat, dat$job, drop = TRUE),
function(x) split(x, x[['name']], drop = TRUE))

给出:

> nestedlist
$administrative
$administrative$hilma
job name phonenumber
11 administrative hilma 651

$administrative$krissy
job name phonenumber
10 administrative krissy 984

$administrative$otis
job name phonenumber
12 administrative otis 325

$administrative$piper
job name phonenumber
13 administrative piper 654

$administrative$rebeca
job name phonenumber
9 administrative rebeca 357


$developer
$developer$albert
job name phonenumber
3 developer albert 987

$developer$dan
job name phonenumber
7 developer dan 951

$developer$john
job name phonenumber
1 developer john 654

$developer$mike
job name phonenumber
2 developer mike 321

$developer$shean
job name phonenumber
8 developer shean 841


$manager
$manager$anna
job name phonenumber
6 manager anna 936

$manager$corliss
job name phonenumber
15 manager corliss 321

$manager$dana
job name phonenumber
4 manager dana 741

$manager$guy
job name phonenumber
5 manager guy 852

$manager$mendy
job name phonenumber
14 manager mendy 984


使用数据:
dat <- structure(list(job = c("developer", "developer", "developer", "manager", "manager", "manager", "developer", "developer", "administrative", "administrative", "administrative", "administrative", "administrative", "manager", "manager"),
name = c("john", "mike", "albert", "dana", "guy", "anna", "dan", "shean", "rebeca", "krissy", "hilma", "otis", "piper", "mendy", "corliss"),
phonenumber = c(654L, 321L, 987L, 741L, 852L, 936L, 951L, 841L, 357L, 984L, 651L, 325L, 654L, 984L, 321L)),
.Names = c("job", "name", "phonenumber"), class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"))

关于r - 数据框到嵌套列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46407320/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com