r - 对于 lapply 中不同长度的列表

标签 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 的每个元素使用 FAFA也是一个列表,然后我使用了 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

最佳答案

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

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.

我认为您使用 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的解决方法:

为了能够执行多个测试并分配相应的值 wo 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])

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

关于r - 对于 lapply 中不同长度的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46825586/

相关文章:

r - 错误 : memory exhausted (limit reached? )//错误 : cannot allocate vector of size 1. 3 Mb

r - 按两个数字对列名称进行排序

R 整洁的行意味着来自列的子集

r - 我们如何通过管道工 web api 返回 ggplot 图?

r - 使用合并语句时出现重复列

r - 计算所有列的总和并创建最高频率的频率图

r - 矩阵操作: set all elements to 0 except for row maximum (maxima)

python - 将列添加到 rpy2 中的 DataFrame

R ggplot log 对数刻度

r - 如何在 R 中使用相对路径从 mac 上的目录读取数据?