我有一个包含命名元素的列表列表(将它们称为“子列表”以避免混淆)。并非所有子列表都包含所有命名元素。我希望将缺少元素的子列表添加为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个子列表分别缺少c
和b
个元素。我希望这些元素可以作为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个出色的解决方案,其中子列表元素为int
。但元素可以是chr
,甚至是列表! E.g:
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))
答案 0 :(得分:1)
更新:我们可以创建唯一的名称,然后遍历列表并对这些名称进行子集化。不在列表中的名称将返回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
<强>原始强> 我们可以将子列表视为dataframe和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
答案 1 :(得分:0)
当然有一种更好的方法可以做到这一点,但这适用于两个例子。
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
希望它有所帮助。