简体   繁体   English

从 r 的嵌套列表中的第一项的总和中减去一个

[英]Subtract one from the sum of the first item in a nested list in r

I have an initial nested list (list of lists) of vectors of integers with NA values randomly replacing some integers.我有一个整数向量的初始嵌套列表(列表列表),其中NA值随机替换一些整数。 Within a nested list, if one vector contains all NA values, it needs to be broken up into two lists (or more, if more than one vector in the nested list contains all NA s).在嵌套列表中,如果一个向量包含所有NA值,则需要将其分解为两个列表(或者更多,如果嵌套列表中的多个向量包含所有NA )。 I ultimately need a vector of values that sums the length nested lists minus 1, ie sum(lengths(list[[i]]-1 , where i is the list of vectors in the nested list, and removes any values less than or equal to 0.我最终需要一个值向量,它将嵌套列表的长度总和减去 1,即sum(lengths(list[[i]]-1 ,其中i是嵌套列表中的向量列表,并删除任何小于或等于的值为 0。

So far I have been able to do this, but realized that if a list is 'artificially' broken into 2+ lists, I only need to subtract one from the first position of the nested list.到目前为止,我已经能够做到这一点,但意识到如果一个列表被“人为”分成 2 个以上的列表,我只需要从嵌套列表的第一个 position 中减去一个。 Furthermore, if the first position of the nested list is NA , the subsequent lists in the nested list do not need to be subtracted by 1.此外,如果嵌套列表的第一个 position 为NA ,则嵌套列表中的后续列表不需要减 1。

Below is some sample code that provides examples of the full nested list, the nested list with NA values randomly assigned, and the final desired vector of sums for the example lists.下面是一些示例代码,提供了完整嵌套列表的示例、随机分配了 NA 值的嵌套列表以及示例列表的最终所需的总和向量。

#Full List
L.full<-list(list(1,3,c(0,2,0),c(0,0)),list(1,6,c(0,3,2,0,1,0),c(0,0,0,1,0,0),1,2,c(0,1),2,c(0,0)),
             list(1,0),list(1,0),list(1,4,c(2,0,0,0),c(4,1),c(1,0,0,0,0),0),list(1,0))
#Nested list with "random" NAs
L.miss<-list(list(1,3,c(0,NA,0),c(0,0)),list(1,6,c(0,3,NA,0,NA,0),c(0,NA,0,1,0,0),1,NA,c(0,1),2,c(0,0)),
             list(1,NA),list(1,0),list(1,NA,c(NA,0,0,0),c(NA,NA),c(1,0,0,NA,0),0),list(1,0))
#Desired final output
L.want<-c(5,11,5,1,3,5,1)

The below code may be a bit inelegant but almost gets me where I need to be;下面的代码可能有点不雅,但几乎可以让我到达我需要的地方; it outputs the final vector as [5,11,4,1,2,4,1] , not the desired [5,11,5,1,3,5,1] .它将最终向量输出为[5,11,4,1,2,4,1] ,而不是所需的[5,11,5,1,3,5,1] How can I have the code subtract one from just the first element in the list, if it is present?如果存在,我怎样才能让代码从列表中的第一个元素中减去一个?

#Break apart 
test<-lapply(lapply(seq_along(L.miss), function(nm) {split(L.miss[[nm]], cumsum(sapply(L.miss[[nm]], function(x) all(is.na(x)))))}), function(lstA) lapply(lstA,function(x) Filter(function(y) !all(is.na(y)), x)))
#Bring the nested list up a level
test2<-unlist(test,recursive=FALSE)
#Remove NA values
test3<-rapply(test2,function(x) x[!is.na(x)], how="replace") #remove NAs
#Sum nested lists
test4<-integer()
for (i in 1:length(test3)){
  test4[i]<-sum(lengths(test3[[i]]))-1
} 
test5<-test4[test4>0] #remove values <=0

Thank you - if this question is too specific for this forum, please let me know and I will remove it.谢谢 - 如果这个问题对于这个论坛来说太具体了,请告诉我,我会删除它。

I think I solved it - it was much simpler to delete the first position in the initial L.miss nested list then ignore the whole "minus 1" altogether:我想我解决了它 - 删除初始L.miss嵌套列表中的第一个 position 然后完全忽略整个“减 1”要简单得多:

#Delete first instance
l<-L.miss
for (i in 1:length(l)){
  l[[i]][[1]]<-NULL
}

#Same code as above but for loop sums without -1
test<-lapply(lapply(seq_along(l), function(nm) {split(l[[nm]], cumsum(sapply(l[[nm]], function(x) all(is.na(x)))))}), function(lstA) lapply(lstA,function(x) Filter(function(y) !all(is.na(y)), x)))
test2<-unlist(test,recursive=FALSE)
test3<-rapply(test2,function(x) x[!is.na(x)], how="replace") #remove NAs
test4<-integer()
for (i in 1:length(test3)){
  test4[i]<-sum(lengths(test3[[i]]))
} 
test5<-test4[test4>0] #remove values <=0

#result
#> test5
#[1]  5 11  5  1  3  5  1

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

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