对于在lapply中具有不同长度的列表

时间:2017-10-19 08:24:26

标签: r

我有复杂的功能,包括许多不同长度的列表。我想使用lapply在这些列表上传递相同的函数,但我得到NULL。我知道我的问题,但不知道如何解决它。

我的第一个问题是:

AFA <- lapply(1:m, function(i) A[[i]][AA[[i]]])

AFA是两个元素的列表。列表的每个元素包含10个元素。

FA <- lapply(1:m, function(i) { which(AFA[[i]][[j]] %in% c(2, 7))})

FA尝试查看10个元素中的哪些元素是2或7.

我知道lapply这里没有帮助。但在这种情况下我无法使用mapply

我的第二个问题是:

`lapply(nAA2, function(x) if (x > 0) { ##if the length of the function is not zero.

      for(j in 1:m){
        for (i in 1:x) ....` 

nAA2是两个元素的列表。第一个元素的长度为1,而第二个元素的长度为2。因此,代码说:

表示第一个元素的长度。 if (AFA[[j]][[FA[[i]]]] == 2)这意味着,使用AFA检查FA的每个元素,因为FA也是一个列表,然后我使用FA[[i]]

而是说:

for ( i in 1:length(nAA2[[1]]) ....for ( i in 1:length(nAA2[[2]]) ....

我想在一个代码中合并。

以下是我的完整代码,其中的注释解释了代码行:

A1 <- c(0, 2, 3, 4, 4,
        0, 0, 3, 4, 1,
        0, 0, 0, 4, 1,
        0, 0, 0, 0, 3,
        0, 0, 0, 0, 0)
A1  <- matrix(A1, 5, 5)
A2 <- c(0, 2, 3, 2, 4,
        0, 0, 3, 4, 1,
        0, 0, 0, 4, 1,
        0, 0, 0, 0, 3,
        0, 0, 0, 0, 0)
A2  <- matrix(A2, 5, 5)

A <- list(A1, A2)
m <- length(A)

AA <- lapply(A, function(x) x > 0)
AA2 <- lapply(A, function(x) x %in% c(2, 7))

AA[is.na(AA)] <- FALSE
AA2[is.na(AA2)] <- FALSE

nAA <- lapply(AA, function(x) sum(x, na.rm = TRUE))
nAA2 <- lapply(AA2, function(x) sum(x, na.rm = TRUE))

AFA <- lapply(1:m, function(i) A[[i]][AA[[i]]])
llA <- lapply(1:m, function(i) double(nAA[[i]]+nAA2[[i]]))
luA <- lapply(1:m, function(i) double(nAA[[i]]+nAA2[[i]]))
FA <-  lapply(1:m, function(i) { which(AFA[[i]][[j]] %in% c(2, 7))}) ## here 
AFA is a list of two elements. I would like to access the first element of 
the list,  and then check each element. I know this is wrong, but how to fix 
it with `mapply`. 


for(j in 1:m){

}
lapply(nAA2, function(x) if (x > 0) { ##here to check that they are not zero. That is we have number equal to 2 or 7.

  for(j in 1:m){ 
    for (i in 1:x) { #here I wouldl like to loop over the length of the 
    first list and then the second list.
      if (AFA[[j]][[FA[[i]]]] == 2) {
        # t
        llA[[j]][nAA[[i]] + i] <- 2
        luA[[j]][nAA[[i]] + i] <- 5 
      }

预期的输出应该是这样的:

[[1]][[1]]
2
[[1]][[2]]
2


[[2]][[1]]
5
[[2]][[2]]
5

1 个答案:

答案 0 :(得分:3)

部分答案(仅第一个问题):

  

FA&lt; - lapply(1:m,函数(i){其中(AFA [[i]] [[j]]%in%c(2,7))})

     

FA尝试查看10个元素中的哪些元素是2或7。

我认为您使用的是lapply错误的方法。 lapply遍历列表中的每个对象,因此要识别2或7的向量元素,只需使用

FA <-  lapply(AFA, function(x) which(x %in% c(2, 7)))

> FA
[[1]]
[1] 1

[[2]]
[1] 1 3

输出显示列表AFA的两个向量中向量元素的位置为2或7。

问题2的解决方案:

为了能够执行多项测试并分配相应的值luAllA,我们首先创建新的列表/向量。

一个例子:

testvalues <- list(2, c(7, 10), c(3, 5))
llAvalues <- c(2, 1, 3)
luAvalues <- c(5, 2, 4)

这些包括要在testvalues中测试的值,以及为相应测试提供给llAluA的值。新对象必须具有相同的长度!

现在我们使用嵌套的mapply调用来遍历外层的AFA,并通过内层的每个测试。

llA <- mapply(function(v, w) mapply(function(x,y) ifelse(v[w] %in% x, y, 0), testvalues, llAvalues), AFA, FA)
luA <- mapply(function(v, w) mapply(function(x,y) ifelse(v[w] %in% x, y, 0), testvalues, luAvalues), AFA, FA)

之后,我们会删除所有生成的0(也可以使用NA)。

llA <- lapply(llA, function(x) x[x != 0])

给我们留下了所需的输出。