使用mapply索引嵌套列表的值

时间:2014-06-26 15:36:33

标签: r mapply

我有一个列表列表,每个子列表包含3个值。我的目标是以系统的方式遍历这个嵌套列表的每个值(即从列表1开始,遍历所有3个值,转到列表2,依此类推),将函数应用于每个值。但是我的函数遇到了缺失的值和中断,我已经将问题追溯到索引本身,这不符合我的预期。列表构造如下:

pop <- 1:100
treat.temp <- NULL
treat <- NULL

## Generate 5 samples of pop
for (i in 1:5){
    treat.temp <- sample(pop, 3)
    treat[[i]] <- treat.temp
}

## Create a list with which to index mapply
iterations <- (1:5)

说明性功能和结果。

test.function <- function(j, k){
  for (n in 1:3){
    print(k[[n]][j])
  }
}

results <- mapply(test.function, iterations, treat)

[1] 61
[1] 63
[1] 73
[1] NA
[1] NA
[1] NA
[1] NA
[1] NA
<snipped>

对于通过'j'的第一个循环,这是有效的。但之后会抛出NAs。但如果我手动完成,它会返回我期望的值。

> print(treat[[1]][1])
[1] 61
> print(treat[[1]][2])
[1] 63
> print(treat[[1]][3])
[1] 73
> print(treat[[2]][1])
[1] 59
> print(treat[[2]][2])
[1] 6
> print(treat[[2]][3])
[1] 75
<snipped>

我确定这是一个基本问题,但我似乎找不到合适的搜索字词来在这里或Google上找到答案。提前谢谢!

编辑添加: MrFlick的答案适用于我的问题。在我的实际使用中,我有多个列表输入(因此是mapply)。一个更详细的例子,带有一些注释。

pop <- 1:100
years <- seq.int(2000, 2014, 1)

treat.temp <- NULL
treat <- NULL
year.temp <- NULL
year <- NULL

## Generate 5 samples of treated states, control states and treatment years 
for (i in 1:5){
  treat.temp <- sample(pop, 20)
  treat[[i]] <- treat.temp

  year.temp <- sample(years, 1)
  year[[i]] <- year.temp
}

## Create a list with which to index mapply
iterations <- (1:5)

## Define function 
test.function <- function(j, k, l){
  for (n in 1:3){

    ## Cycles treat through each value of jXn
    print(k[n])
    ## Holds treat (k) fixed for each 3 cycle set of n (using first value in each treat sub-list); cycles through sub-lists as j changes
    print(k[1])
    ## Same as above, but with 2nd value in each sub-list of treat
    print(k[2])
    ## Holds year (l) fixed for each 3 cycle set of n, cycling through values of year each time j changes
    print(l[1])
    ## Functionally equivalent to
    print(l)
  }
}

results <- mapply(test.function, iterations, treat, year)

1 个答案:

答案 0 :(得分:2)

好吧,你可能会误解mapply的工作原理。该函数将遍历您作为参数传递的两个迭代,这意味着treat也将是每次迭代的子集。基本上,被调用的函数是

test.function(iterations[1], treat[[1]])
test.function(iterations[2], treat[[2]])
test.function(iterations[3], treat[[3]])
...

并且您似乎将k变量视为整个列表。此外,您的索引也会向后退。但只是为了让你的测试工作,你可以做到

test.function <- function(j, k){
  for (n in 1:3) print(k[n]) 
}

results <- mapply(test.function, iterations, treat)

但这并不是一个非常棒的迭代列表的方法。你到底想要完成什么?