[英]for over list with different lengths in lapply
I have complicated function which includes many lists with different lengths. 我有复杂的功能,包括许多不同长度的列表。 I would like to pass the same function over these lists using
lapply
but I got NULL
. 我想使用
lapply
在这些列表上传递相同的函数,但我得到了NULL
。 I know my problem but do not know how to fix it. 我知道我的问题,但不知道如何解决它。
My first problem is here: 我的第一个问题是:
AFA <- lapply(1:m, function(i) A[[i]][AA[[i]]])
AFA is a list of two elements. AFA是两个元素的列表。 Each element of list contains 10 elements.
列表的每个元素包含10个元素。
FA <- lapply(1:m, function(i) { which(AFA[[i]][[j]] %in% c(2, 7))})
FA tried to see which elements of the 10 elements are 2 or 7. FA试图看出10个元素中的哪些元素是2或7。
I know that lapply
here is not help. 我知道
lapply
这里没有帮助。 But I could not use mapply
in this case. 但在这种情况下我无法使用
mapply
。
My second problem is: 我的第二个问题是:
`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
is a list of two elements. nAA2
是两个元素的列表。 The length of the first element is 1
while 2
for the second element. 第一个元素的长度为
1
而第二个元素的长度为2
。 Hence, the code says this: 因此,代码说:
for the length of the first element. 对于第一个元素的长度。
if (AFA[[j]][[FA[[i]]]] == 2)
this means, check each element of AFA
using FA
and since FA
is also a list then I used FA[[i]]
. if (AFA[[j]][[FA[[i]]]] == 2)
这意味着,使用FA
检查AFA
每个元素,因为FA
也是一个列表,然后我使用FA[[i]]
。
Instead to say: 而是说:
for ( i in 1:length(nAA2[[1]]) ....
and for ( i in 1:length(nAA2[[2]]) ....
for ( i in 1:length(nAA2[[1]]) ....
和for ( i in 1:length(nAA2[[2]]) ....
I would like to merge then in one code. 我想在一个代码中合并。
Here is my full code with my comments explaining the code lines: 这是我的完整代码,我的评论解释了代码行:
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
}
The expected output should be like this: 预期的输出应该是这样的:
[[1]][[1]]
2
[[1]][[2]]
2
[[2]][[1]]
5
[[2]][[2]]
5
Partial answer (only first problem): 部分答案(仅第一个问题):
FA <- lapply(1:m, function(i) { which(AFA[[i]][[j]] %in% c(2, 7))})
FA < - lapply(1:m,函数(i){其中(AFA [[i]] [[j]]%in%c(2,7))})
FA tried to see which elements of the 10 elements are 2 or 7.
FA试图看出10个元素中的哪些元素是2或7。
I think you are using lapply
the wrong way. 我认为你正以错误的方式使用
lapply
。 lapply
loops over every object in a list, so to identify the vector elements which are either 2 or 7, just use lapply
循环遍历列表中的每个对象,因此要识别2或7的向量元素,只需使用
FA <- lapply(AFA, function(x) which(x %in% c(2, 7)))
> FA
[[1]]
[1] 1
[[2]]
[1] 1 3
The output shows you the positions of vector elements that are either 2 or 7 in the both vectors of list AFA
. 输出显示列表
AFA
的两个向量中2或7的向量元素的位置。
Solution for problem 2: 问题2的解决方案:
To be able to perform multiple tests and assign the corresponding values wo luA
and llA
, we first create new lists/vectors. 为了能够执行多种测试和WO分配相应的值
luA
和llA
,我们首先创建一个新的列表/矢量。
An example: 一个例子:
testvalues <- list(2, c(7, 10), c(3, 5))
llAvalues <- c(2, 1, 3)
luAvalues <- c(5, 2, 4)
These include the values to be tested for in testvalues
, and the values to be given to llA
and luA
for the corresponding tests. 这些包括用于将待测试的值
testvalues
,并且必须考虑到这些值llA
和luA
用于相应的测试。 The new objects must have the same lengths! 新对象必须具有相同的长度!
Now we use a nested mapply
call to loop through AFA
on the outer level, and through each test on the inner level. 现在我们使用嵌套的
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)
Afterwards, we delete all generated 0
(could also use NA
). 之后,我们删除所有生成的
0
(也可以使用NA
)。
llA <- lapply(llA, function(x) x[x != 0])
which leaves us with the desired output. 这给我们留下了所需的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.