R 如何以良好的方式显示列表列表

R 如何以良好的方式显示列表列表,r,list,R,List,我有一个列表,如下所示 每个列表(例如列表1、列表2、列表3)都有两个属性:变量和时间 list1 <- list(c("Color", "Price"), "Quarter") list2 <- list(c("Price"), "Month") list3 <- list(c("Color"), "Month") total <- list(list1, list2, list3) 如何将其转换为这样的数据帧 EDIT: I am able to accomplis

我有一个列表,如下所示

每个列表(例如列表1、列表2、列表3)都有两个属性:变量和时间

list1 <- list(c("Color", "Price"), "Quarter")
list2 <- list(c("Price"), "Month")
list3 <- list(c("Color"), "Month")
total <- list(list1, list2, list3)
如何将其转换为这样的数据帧

EDIT: I am able to accomplish it using this code. Any better suggestion is appreciated!

  num <- length(total)
  max      <- 0
  for(i in 1:num) {
    if(length(total[[i]][1]) > max) {
      max <- length(total[[i]])
    }
  }

  for(i in 1:num) {
    length(total[[i]][[1]]) <- max
    for(j in 1:max) {
      if(is.null(total[[i]][[1]][[j]])) {
        total[[i]][[1]][[j]] <- " "
      }
    }
  }

  df <- data.frame(matrix(unlist(total), nrow=num, byrow=T))

EDIT:我可以使用此代码完成它。如果有更好的建议,我们将不胜感激!

num这不仅仅是一个嵌套的问题,它是一个嵌套的问题。如果我对事物的理解是正确的,那么颜色和价格在一个列表中,而季度在另一个列表中这一事实是有意义的。因此,实际上,您应该了解如何将每个列表的第一个元素转换为
data.frame
,对所有其他元素重复,然后合并结果。(这就是@divibisan和@camille的建议发挥作用的地方……减少问题,使用重复的代码,然后合并。)

(事实上,我相信每个列表中的元素不会超过两个,这并不是一个严格的因素。下面是处理1个或多个元素的一般方法,而不仅仅是“始终2”。)

您的数据:

str(total)
# List of 3
#  $ :List of 2
#   ..$ : chr [1:2] "Color" "Price"
#   ..$ : chr "Quarter"
#  $ :List of 2
#   ..$ : chr "Price"
#   ..$ : chr "Month"
#  $ :List of 2
#   ..$ : chr "Color"
#   ..$ : chr "Month"
我们需要做的是按每个列表的元素进行分解。(我假设这里会有对称性。)让我们从每个元素的第一个元素开始:

total1 <- lapply(total, `[[`, 1)
str(total1)
# List of 3
#  $ : chr [1:2] "Color" "Price"
#  $ : chr "Price"
#  $ : chr "Color"
现在这是一个
矩阵
,不是
数据帧
,但这是一个开始。让我们在最后讨论命名。让我们编写一个函数来完成刚才所做的工作,然后在
total
的每个级别上使用它

但是,为了做到这一点,我们需要修改
total
,以便新的第一个元素包含所有的第一个元素,新的第二个元素包含所有的秒,等等

newtotal <- lapply(seq_len(max(sapply(total, length))), function(i) lapply(total, `[[`, i))
str(newtotal)
# List of 2
#  $ :List of 3
#   ..$ : chr [1:2] "Color" "Price"
#   ..$ : chr "Price"
#   ..$ : chr "Color"
#  $ :List of 3
#   ..$ : chr "Quarter"
#   ..$ : chr "Month"
#   ..$ : chr "Month"
m <- do.call(cbind, lapply(newtotal, func))
m
#      [,1]    [,2]    [,3]     
# [1,] "Color" "Price" "Quarter"
# [2,] "Price" NA      "Month"  
# [3,] "Color" NA      "Month"  

这不仅仅是一个嵌套的问题,而是一个嵌套的问题。如果我对事物的理解是正确的,那么颜色和价格在一个列表中,而季度在另一个列表中这一事实是有意义的。因此,实际上,您应该了解如何将每个列表的第一个元素转换为
data.frame
,对所有其他元素重复,然后合并结果。(这就是@divibisan和@camille的建议发挥作用的地方……减少问题,使用重复的代码,然后合并。)

(事实上,我相信每个列表中的元素不会超过两个,这并不是一个严格的因素。下面是处理1个或多个元素的一般方法,而不仅仅是“始终2”。)

您的数据:

str(total)
# List of 3
#  $ :List of 2
#   ..$ : chr [1:2] "Color" "Price"
#   ..$ : chr "Quarter"
#  $ :List of 2
#   ..$ : chr "Price"
#   ..$ : chr "Month"
#  $ :List of 2
#   ..$ : chr "Color"
#   ..$ : chr "Month"
我们需要做的是按每个列表的元素进行分解。(我假设这里会有对称性。)让我们从每个元素的第一个元素开始:

total1 <- lapply(total, `[[`, 1)
str(total1)
# List of 3
#  $ : chr [1:2] "Color" "Price"
#  $ : chr "Price"
#  $ : chr "Color"
现在这是一个
矩阵
,不是
数据帧
,但这是一个开始。让我们在最后讨论命名。让我们编写一个函数来完成刚才所做的工作,然后在
total
的每个级别上使用它

但是,为了做到这一点,我们需要修改
total
,以便新的第一个元素包含所有的第一个元素,新的第二个元素包含所有的秒,等等

newtotal <- lapply(seq_len(max(sapply(total, length))), function(i) lapply(total, `[[`, i))
str(newtotal)
# List of 2
#  $ :List of 3
#   ..$ : chr [1:2] "Color" "Price"
#   ..$ : chr "Price"
#   ..$ : chr "Color"
#  $ :List of 3
#   ..$ : chr "Quarter"
#   ..$ : chr "Month"
#   ..$ : chr "Month"
m <- do.call(cbind, lapply(newtotal, func))
m
#      [,1]    [,2]    [,3]     
# [1,] "Color" "Price" "Quarter"
# [2,] "Price" NA      "Month"  
# [3,] "Color" NA      "Month"  

这两个建议副本中提供的答案都不适用于此数据,因为列表的长度不同。不清楚为什么(例如)这里的第一个列表在一个嵌套列表中有2个元素,而在另一个嵌套列表中有1个元素,但是它在建议的输出中是同质显示的。也许能说清楚点,凯蒂?好吧,凯蒂,我明白你为什么要做次嵌套了。是的,坦白说,这很难说。你能控制数据的格式吗?可能有更有效和数据友好的方式来存储此类信息。(@divibisan和@camille,你的重复建议为时过早,我建议你删除它们。)r2evans。不幸的是,我没有原始脚本。我正在努力利用我所拥有的。知道吗?不是用COBOL写的,是吗?多年来,我发现许多问题都是持续存在的,因为没有多少人对COBOL感兴趣,也没有多少人对尝试重构软件犹豫不决:-)(我相信不是这样的…COBOL对这样的列表并没有一个好的概念…)可能的重复事实上,这两个建议的重复中提供的答案都不适用于这些数据,因为列表的长度不同。不清楚为什么(例如)这里的第一个列表在一个嵌套列表中有2个元素,而在另一个嵌套列表中有1个元素,但是它在建议的输出中是同质显示的。也许能说清楚点,凯蒂?好吧,凯蒂,我明白你为什么要做次嵌套了。是的,坦白说,这很难说。你能控制数据的格式吗?可能有更有效和数据友好的方式来存储此类信息。(@divibisan和@camille,你的重复建议为时过早,我建议你删除它们。)r2evans。不幸的是,我没有原始脚本。我正在努力利用我所拥有的。知道吗?不是用COBOL写的,是吗?我发现这些年来有很多问题一直存在,因为没有多少人喜欢COBOL,并且对尝试重构软件犹豫不决:-)(我相信不是这样的…COBOL对这样的列表并没有很好的概念…)
newtotal <- lapply(seq_len(max(sapply(total, length))), function(i) lapply(total, `[[`, i))
str(newtotal)
# List of 2
#  $ :List of 3
#   ..$ : chr [1:2] "Color" "Price"
#   ..$ : chr "Price"
#   ..$ : chr "Color"
#  $ :List of 3
#   ..$ : chr "Quarter"
#   ..$ : chr "Month"
#   ..$ : chr "Month"
m <- do.call(cbind, lapply(newtotal, func))
m
#      [,1]    [,2]    [,3]     
# [1,] "Color" "Price" "Quarter"
# [2,] "Price" NA      "Month"  
# [3,] "Color" NA      "Month"  
m <- do.call(cbind, lapply(newtotal, func))
colnames(m) <- c(paste0("Var", seq_len(ncol(m)-1L)), "Time")
df <- as.data.frame(m)
df$List <- paste0('List', seq_len(nrow(df)))
df
#    Var1  Var2    Time  List
# 1 Color Price Quarter List1
# 2 Price  <NA>   Month List2
# 3 Color  <NA>   Month List3