Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 用于不同长度的重叠列表_R - Fatal编程技术网

R 用于不同长度的重叠列表

R 用于不同长度的重叠列表,r,R,我有一个复杂的函数,它包含许多不同长度的列表。我想使用lappy在这些列表上传递相同的函数,但是我得到了NULL。我知道我的问题,但不知道如何解决它 我的第一个问题是: AFA部分答案(仅第一个问题): FA这一切都有点神秘,你能展示你的预期输出吗?是的,这很有效。谢谢。第二个问题呢。我编辑我的问题是为了让它变得清晰。你能解释一下for循环中的索引吗?AFA、FA、llA和luA中的位置如何配合?我很确定有一个更简单的解决方案,但我不明白您希望如何为应该更改的单元格编制索引。请这样解释:检查列表

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

我的第一个问题是:

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


FA这一切都有点神秘,你能展示你的预期输出吗?是的,这很有效。谢谢。第二个问题呢。我编辑我的问题是为了让它变得清晰。你能解释一下for循环中的索引吗?
AFA
FA
llA
luA
中的位置如何配合?我很确定有一个更简单的解决方案,但我不明白您希望如何为应该更改的单元格编制索引。请这样解释:检查列表
AFA
向量中的哪些元素是2(
if(AFA[[j]][[FA[[I]]]]]==2)
),但是应该更改列表
llA
luA
中的哪些元素?在我看来,
AFA
有两个向量,每个向量长度为10,但是
llA
luA
都有两个向量长度为11和12。我更新了第二个问题的问题。第二个问题的问题。我想分别循环每个列表中每个元素的长度。第一个元素的长度,完成所有作业,然后为第二个元素运行。例如,第一个元素的长度为1,而第二个元素的长度为2。我希望您能帮助我,然后,我可以接受您的答案。请等待接受,您的问题尚未得到完全回答。我仍然很难理解你的第二个问题。我不知道如何感谢你。这真是一个了不起的帮助。
`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) ....` 
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
FA <-  lapply(AFA, function(x) which(x %in% c(2, 7)))

> FA
[[1]]
[1] 1

[[2]]
[1] 1 3
testvalues <- list(2, c(7, 10), c(3, 5))
llAvalues <- c(2, 1, 3)
luAvalues <- c(5, 2, 4)
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)
llA <- lapply(llA, function(x) x[x != 0])