gpt4 book ai didi

r - 取消列出嵌套列表并使用 ggplot 绘图

转载 作者:行者123 更新时间:2023-12-03 16:10:56 26 4
gpt4 key购买 nike

我有一个自己制作的可怕的嵌套列表结构,如下所示:

str(CMaster)
List of 4
$ :List of 6
..$ :List of 5
.. ..$ :List of 15
.. .. ..$ : num [1, 1:14] 0.144 0.2 0.256 0.352 0.446 ...
.. .. ..$ : num [1, 1:47] 0.144 0.2 0.375 0.54 0.694 ...
etc
$ :List of 6
..$ :List of 1
.. ..$ :List of 15
.. .. ..$ : num [1, 1:14] 0.144 0.2 0.256 0.352 0.446 ...
.. .. ..$ : num [1, 1:47] 0.144 0.2 0.375 0.54 0.694 ...
结构是固定的,但最后一个 15 个列表可能会达到 150K,我需要尝试绘制这个结构。我想尝试绘制按 List of 4 变量分类的箱线图,用于将 List of 15 的所有数字数据压缩到此示例中的 List of 6 中的每一个。我需要先把它全部取消吗?有没有更简单的方法来制作 data.frame 或 data.table 来保留列表的名称并使它们成为绘图的因素?
dfs <- lapply(CMaster, data.frame, stringsAsFactors = FALSE)
编辑:我添加了示例代码
示例代码(接近真实结构)。
D<-list()
DNSIM<-list()
DTime<-list()
DMaster<-list()


for(CC in 1:4){
for(t in 1:6){
for(N in 1:5){
for(i in 1:15){

Dmatrix=runif(15)
D[[i]]=Dmatrix
}
DTime[[t]]=D
}
DNSIM[[N]]=DTime
}
DMaster[[CC]]=DTime
}
enter image description here
输入
它太大而无法复制,我的组织不允许与 onedrive 共享链接。有什么简单的解决方法吗?
编辑2
tibble(lists = CMaster) %>% 
+ mutate(CleaningType = row_number()) %>%
+ unnest_longer(lists, indices_to = "TimePoint") %>%
+ unnest_longer(lists, indices_to = "Replicate") %>%
+ unnest_longer(lists, indices_to = "BehaviourObservation")
# A tibble: 1,800 x 5
lists BehaviourObservation Replicate TimePoint CleaningType
<list> <int> <int> <int> <int>
1 <dbl[,14] [1 × 14]> 1 1 1 1
2 <dbl[,47] [1 × 47]> 2 1 1 1
3 <dbl[,11] [1 × 11]> 3 1 1 1
4 <dbl[,40] [1 × 40]> 4 1 1 1
5 <dbl[,40] [1 × 40]> 5 1 1 1
6 <dbl[,34] [1 × 34]> 6 1 1 1
7 <dbl[,92] [1 × 92]> 7 1 1 1
8 <dbl[,31] [1 × 31]> 8 1 1 1
9 <dbl[,5] [1 × 5]> 9 1 1 1
10 <dbl[,103] [1 × 103]> 10 1 1 1
# … with 1,790 more rows
因此,我尝试添加另一个子子列表,但现在出现大小不兼容的错误。请问对此有什么想法吗?
tibble(lists = CMaster) %>% 
+ mutate(CleaningType = row_number()) %>%
+ unnest_longer(lists, indices_to = "TimePoint") %>%
+ unnest_longer(lists, indices_to = "Replicate") %>%
+ unnest_longer(lists, indices_to = "BehaviourObservation") %>%
+ unnest_longer(lists, indices_to = "sub_sub_observation")

Error: Can't combine `..1$lists` <double[,14]> and `..2$lists` <double[,47]>.
✖ Incompatible sizes 14 and 47 along axis 2.
Run `rlang::last_error()` to see where the error occurred.

最佳答案

如果您不介意使用 tidyverse,请在下面找到一些代码来使用 tidyr::unnest_longer 对数据进行矩形化处理。 .见 here有关如何使用的很好的教程 unnest_longer (以及一般如何将嵌套列表转换为 data.frames)。
我不确定 observation 和有什么区别和 sub_observation结果,如果这个情节是你真正想要的。
这在您的大型数据集上可能(太)慢。

library(tidyverse)

df <- tibble(lists = DMaster) %>%
mutate(facet = row_number()) %>%
unnest_longer(lists, indices_to = "boxplot") %>%
unnest_longer(lists, indices_to = "observation") %>%
unnest_longer(lists, indices_to = "sub_observation")

df %>%
ggplot(aes(boxplot, lists, group = boxplot)) +
geom_boxplot() +
facet_wrap(~ facet)
这给出了一个带有 facet 的 data.frame (1 到 4), boxplot (1 到 6), observation (1 到 15), sub_observation (1 到 15)和 lists (您的实际数值),以及下图:

关于r - 取消列出嵌套列表并使用 ggplot 绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62949713/

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