【问题标题】:R - Recursively find number of sub listsR - 递归查找子列表的数量
【发布时间】:2019-12-01 04:42:03
【问题描述】:

我有一个列表列表(我们称之为主/父列表 A)。列表 A 中的子列表数量不同:A[[1]] 可以有 1 个子列表,而 A[[14344]] 可以有 6 个子列表。我需要找到 A 的每个元素的子列表的数量。目标是找到或编写一个返回 A[1] = 1A[14344] = 6 的函数。

单独使用 lengthlengths 对我不起作用 - 请参阅下面的示例:

A[1] # element 1 of list A contains 1 sub-list with 5 values
   [,1]
[1,]    2
[2,]    3
[3,]    4
[4,]    5
[5,]    6


A[2] # element 2 of list A contains 5 sub-lists with 5,2,2,3,4 values (respectively)
[[1]]
[[1]][[1]]
[1] 2 3 4 5 6

[[1]][[2]]
[1] 15 16

[[1]][[3]]
[1] 18 19

[[1]][[4]]
[1] 23 24 25

[[1]][[5]]
[1] 30 31 32 33

# using lengths() returns the entire list for A[1] and the length of each element in list 2 
lengths(A[[1]]))
     [,1]
[1,]    2
[2,]    3
[3,]    4
[4,]    5
[5,]    6

lengths(A[[2]])
[1] 5 2 2 3 4

# using length returns just 1 for both 
length(A[1])
[1] 1
length(A[2])
[1] 1

# structure of sub-list 1
str(A[1])
List of 1
$ : int [1:5, 1] 2 3 4 5 6

# structure of sub-list 2
str(A[2])
List of 1
$ :List of 5
  ..$ : int [1:5] 2 3 4 5 6
  ..$ : int [1:2] 15 16
  ..$ : int [1:2] 18 19
  ..$ : int [1:3] 23 24 25
  ..$ : int [1:4] 30 31 32 33

对于上面的示例,我需要提供A[1] = 1A[2] = 5 的东西。有什么想法吗?

【问题讨论】:

    标签: r list


    【解决方案1】:

    也许你可以使用lengths

    lengths(A)
    

    例如,对于这个列表

    A <- list(a = 1, b = c(1, 2, 34))
    out <- lengths(A)
    out
    #a b 
    #1 3 
    

    所以out[[1]] 返回 1,out[[2]] 返回 3。

    【讨论】:

      【解决方案2】:

      让我们将“test”定义为一个列表,它在索引“a”处有一个列表,该列表又包含列表“b”和“c”。 “test”也会有一个索引“d”,即mtcars数据集:

      test <- list(a = list(b = list(), c = list()), d = mtcars)
      
      str(test)
      
      List of 2
       $ a:List of 2
        ..$ b: list()
        ..$ c: list()
       $ d:'data.frame':  32 obs. of  11 variables:
        ..$ mpg : num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
        ..$ cyl : num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
        ..$ disp: num [1:32] 160 160 108 258 360 ...
        ..$ hp  : num [1:32] 110 110 93 110 175 105 245 62 95 123 ...
        ..$ drat: num [1:32] 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
        ..$ wt  : num [1:32] 2.62 2.88 2.32 3.21 3.44 ...
        ..$ qsec: num [1:32] 16.5 17 18.6 19.4 17 ...
        ..$ vs  : num [1:32] 0 0 1 1 0 1 0 1 1 1 ...
        ..$ am  : num [1:32] 1 1 1 0 0 0 0 0 0 0 ...
        ..$ gear: num [1:32] 4 4 4 3 3 3 3 4 4 4 ...
        ..$ carb: num [1:32] 4 4 1 1 2 1 4 2 2 4 ...
      

      您描述的函数应该将索引为“a”、“b”和“c”的对象计为列表。索引“d”处的数据框在技术上也是一个列表,但我们可以使用检查来排除它。

      因此,我们的函数应该返回一个值 3。

      countLists <- function(x, count = 0) {
      
        sapply(x, function(y) {
          if (is.list(y) && !is.data.frame(y)) {
            count <<- countLists(y, count + 1)
          }
        })
        return(count)
      }
      
      countLists(test)
      
      3
      

      【讨论】:

        猜你喜欢
        • 2014-08-03
        • 1970-01-01
        • 2013-06-19
        • 2020-11-03
        • 1970-01-01
        • 2020-04-06
        • 2017-03-25
        • 1970-01-01
        • 2019-01-12
        相关资源
        最近更新 更多