在缺少元素的子列表列表中扩充子列表作为 NA

Augment sublists in a list of sublists with missing elements as NA

我有一个包含命名元素的列表列表(将它们称为 "sublists" 以避免混淆)。并非所有子列表都包含所有命名元素。我希望用 NA.

来增加缺少元素的子列表

示例:

l <- list(list(a = 1, b = 2, c = 3),
  list(a = 4, b = 5, c = 6),
  list(a = 7, b = 8),
  list(a = 9, c = 10))

可以看出,第 3 个和第 4 个子列表分别缺少 cb 元素。我希望这些元素被扩充为这些子列表的 NA,即:

res <- list(list(a = 1, b = 2, c = 3),
  list(a = 4, b = 5, c = 6),
  list(a = 7, b = 8, c = NA),
  list(a = 9, b = NA, c = 10))

实际上,如果这样做更容易,每个子列表只缺少最后的 k 个元素(即我没有遇到第 4 个子列表缺少中间元素 b 的情况),但我觉得我们可以找到一个通用的解决方案。

更新: 针对此特定场景获得了 3 个很好的解决方案,其中子列表元素为 ints。但是元素可以是chrs,甚至是列表!例如:

l <- list(list(a = list(1,2), b = 2, c = 3),
      list(b = 5, c = 6),
      list(a = list(5,6), b = 8),
      list(a = list(7,8), c = 10))

a 元素是一个列表,应该保持在 res 列表中。如果它丢失了,我想要一个NA,像往常一样:

res <- list(list(a = list(1,2), b = 2, c = 3),
  list(a = NA, b = 5, c = 6),
  list(a = list(5,6), b = 8, c = NA),
  list(a = list(7,8), b = NA, c = 10))

更新:我们可以创建唯一的名称,然后循环遍历列表并对这些名称进行子集化。不在列表中的名称将 return NULL,那些我们将分配给 NA。这应该适用于所有输入。

# data
l <- list(list(a = list(1,2), b = 2, c = 3),
      list(b = 5, c = 6),
      list(a = list(5,6), b = 8),
      list(a = list(7,8), c = 10))

myNames <- unique(unlist(sapply(l, names)))

res <- lapply(l, function(i){
  x2 <- lapply(myNames, function(j){
    x1 <- i[[ j ]]
    if(is.null(x1)){ x1 <- NA}
    x1
    })
  names(x2) <- myNames
  x2
})

# check results
identical(res,
          #expected output
          list(list(a = list(1,2), b = 2, c = 3),
               list(a = NA, b = 5, c = 6),
               list(a = list(5,6), b = 8, c = NA),
               list(a = list(7,8), b = NA, c = 10)))
# [1] TRUE

原文: 我们可以将 sublist 视为数据框,并在缺少的列上填充 rbind,然后再次拆分:

# data:
l <- list(list(a = list(1,2), b = 2, c = 3),
          list(a = list(3,4), b = 5, c = 6),
          list(a = list(5,6), b = 8),
          list(a = list(7,8), c = 10))

library(dplyr)

# convert to dataframe and rbind with fill on missing columns
x <- bind_rows(lapply(l, as_data_frame))

# then convert it back to list
res <- lapply(split(x, seq(nrow(x))), as.list)

# drop names, we can skip this step if we want to keep names as 1,2,3,4...
names(res) <- NULL

# result
res

# [[1]]
# [[1]]$a
# [1] 1
# 
# [[1]]$b
# [1] 2
# 
# [[1]]$c
# [1] 3
# 
# 
# [[2]]
# [[2]]$a
# [1] 4
# 
# [[2]]$b
# [1] 5
# 
# [[2]]$c
# [1] 6
# 
# 
# [[3]]
# [[3]]$a
# [1] 7
# 
# [[3]]$b
# [1] 8
# 
# [[3]]$c
# [1] NA
# 
# 
# [[4]]
# [[4]]$a
# [1] 9
# 
# [[4]]$b
# [1] NA
# 
# [[4]]$c
# [1] 10

当然有更好的方法,但这对两个示例都适用。

res 和 res2 是您提供的示例结果。

l.res 和 l2.res 是代码的结果。

l <- list(list(a = 1, b = 2, c = 3),
          list(a = 4, b = 5, c = 6),
          list(a = 7, b = 8),
          list(a = 9, c = 10))

res <- list(list(a = 1, b = 2, c = 3),
             list(a = 4, b = 5, c = 6),
             list(a = 7, b = 8, c = NA),
             list(a = 9, b = NA, c = 10))

l2 <- list(list(a = list(1,2), b = 2, c = 3),
          list(b = 5, c = 6),
          list(a = list(5,6), b = 8),
          list(a = list(7,8), c = 10))
res2 <- list(list(a = list(1,2), b = 2, c = 3),
            list(a = NA, b = 5, c = 6),
            list(a = list(5,6), b = 8, c = NA),
            list(a = list(7,8), b = NA, c = 10))


#vector with 'column names' to be checked

aux=c("a","b","c")

#function  that check if all sublists have all the elements
#if not, create the element and asign NA value
myfunction<-function(l.list,n.names){

  for(i in 1:length(l.list)){
    for(j in 1:length(n.names)){
      if (n.names[j] %in% names(l.list[[i]]) == FALSE) {
        l.list[[i]][n.names[j]]<-NA
        l.list[[i]]=l.list[[i]][order(unlist(names(l.list[[i]])))]
       }
     }
    }

  return(l.list)
}

#Applying to example 1
l.res<-myfunction(l,aux)

data.frame(l.res) #as a data frame just for comparison purpose
##   a b c a.1 b.1 c.1 a.2 b.2 c.2 a.3 b.3 c.3
## 1 1 2 3   4   5   6   7   8  NA   9  NA  10
data.frame(res)
##   a b c a.1 b.1 c.1 a.2 b.2 c.2 a.3 b.3 c.3
## 1 1 2 3   4   5   6   7   8  NA   9  NA  10


#Applying to example 2
l2.res<-myfunction(l2,aux)

data.frame(l2.res) #as a data frame just for comparison purpose
##   a.1 a.2 b c  a b.1 c.1 a.5 a.6 b.2 c.2 a.7 a.8 b.3 c.3
## 1   1   2 2 3 NA   5   6   5   6   8  NA   7   8  NA  10
data.frame(res2)
##   a.1 a.2 b c  a b.1 c.1 a.5 a.6 b.2 c.2 a.7 a.8 b.3 c.3
## 1   1   2 2 3 NA   5   6   5   6   8  NA   7   8  NA  10

希望对您有所帮助。