繁体   English   中英

如何将 function 应用于 r 中的多个列表列表?

[英]How to apply a function to more than one list of lists in r?

我有一个包含列表列表的数据。 我想找到最大值。 如图所示,每个双精度类型数据的值;

输出

这是我的数据结构;

list(`Cluster 1` = list(Day_1 = list(structure(c(`1` = 0, `2` = 0, 
    `3` = 0, `4` = 0, `5` = 0, `6` = 0, `7` = 0, `8` = 0, `9` = 0.041, 
    `10` = 0.673, `11` = 0, `12` = 0.766), .Dim = 12L, .Dimnames = list(
        c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", 
        "12"))), structure(c(`1` = 0, `2` = 0, `3` = 0, `4` = 0, 
    `5` = 0, `6` = 0.041, `7` = 0.673, `8` = 0.766), .Dim = 8L, .Dimnames = list(
        c("1", "2", "3", "4", "5", "6", "7", "8")))), Day_2 = list(
        structure(c(`1` = 1.07, `2` = 0, `3` = 1.27, `4` = 0.19, 
        `5` = 0, `6` = 0, `7` = 0, `8` = 0, `9` = 0, `10` = 0, `11` = 0, 
        `12` = 0), .Dim = 12L, .Dimnames = list(c("1", "2", "3", 
        "4", "5", "6", "7", "8", "9", "10", "11", "12"))), structure(c(`1` = 1.07, 
        `2` = 1.27, `3` = 0.19, `4` = 0, `5` = 0, `6` = 0, `7` = 0, 
        `8` = 0), .Dim = 8L, .Dimnames = list(c("1", "2", "3", "4", 
        "5", "6", "7", "8"))))), `Cluster 2` = list(Day_3 = list(
        structure(c(`1` = 0, `2` = 0, `3` = 0, `4` = 0, `5` = 0, 
        `6` = 0, `7` = 0, `8` = 0, `9` = 0.19, `10` = 0, `11` = 0, 
        `12` = 0), .Dim = 12L, .Dimnames = list(c("1", "2", "3", 
        "4", "5", "6", "7", "8", "9", "10", "11", "12"))), structure(c(`1` = 0, 
        `2` = 0, `3` = 0, `4` = 0, `5` = 0, `6` = 0.19, `7` = 0, 
        `8` = 0), .Dim = 8L, .Dimnames = list(c("1", "2", "3", "4", 
        "5", "6", "7", "8")))), Day_4 = list(structure(c(`1` = 0.521, 
    `2` = 0.229, `3` = 0, `4` = 0, `5` = 0, `6` = 0, `7` = 0, `8` = 0, 
    `9` = 0, `10` = 0, `11` = 0, `12` = 0), .Dim = 12L, .Dimnames = list(
        c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", 
        "12"))), structure(c(`1` = 0.75, `2` = 0, `3` = 0, `4` = 0, 
    `5` = 0, `6` = 0, `7` = 0, `8` = 0), .Dim = 8L, .Dimnames = list(
        c("1", "2", "3", "4", "5", "6", "7", "8"))))))

我试过这段代码;

nested_lapply <- function(data, fun) {
    lapply(data, function(sublist) { lapply(sublist, fun) })
}

但我收到了这个错误

res<-nested_lapply(out, max) Error in FUN(X[[i]], ...): invalid 'type' (list) of argument Called from: lapply(sublist, fun)

除了问题:

我需要找到最大值。 所有 [[ 1 ]] 以及 [[2]] 的值。

max1<-max(out$ Cluster 1 $Day_1[ 1 ], out$ Cluster 1 $Day_2[ 1 ], out$ Cluster 2 $Day_3[ 1 ], out$ Cluster 2 $Day_4[ 1 ])

max2<-max(out$ Cluster 1 $Day_1[[2]], out$ Cluster 1 $Day_2[[2]], out$ Cluster 2 $Day_3[[2]], out$ Cluster 2 $Day_4[[2 ]])

我们可以只使用来自base R rapply rapply ,它将递归地循环三个嵌套list并从内部vector中获取max

rapply(out, max)

如果我们想跨越max

library(dplyr)
library(data.table)
reshape2::melt(out) %>% 
   group_by(L3) %>%
   summarise(value = max(value))
# A tibble: 2 x 2
#    L3 value
#  <int> <dbl>
#1     1  1.27
#2     2  1.27

或者它可能是

flatten(out) %>% 
      transpose %>% 
      map(reduce, pmax)
#[[1]]
#    1     2     3     4     5     6     7     8     9    10    11    12 
#1.070 0.229 1.270 0.190 0.000 0.000 0.000 0.000 0.190 0.673 0.000 0.766 

#[[2]]
#    1     2     3     4     5     6     7     8 
#1.070 1.270 0.190 0.000 0.000 0.190 0.673 0.766 

或单个值

flatten(out) %>% 
    transpose %>%
    map_dbl(reduce, max)
#[1] 1.27 1.27

你可以使用这个解决方案

library(tidyverse)
library(purrr)
df %>% 
  enframe() %>% 
  unnest_longer(value) %>% 
  unnest_longer(value) %>% 
  transmute(name, value_id, out = map_dbl(value, max))

你可以做这样的事情

lapply(1:length(data), function(X) lapply(data[[X]], FUN))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM