在改变参数的情况下使用lapply [英] Using lapply with changing arguments

查看:63
本文介绍了在改变参数的情况下使用lapply的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R教科书继续促进使用lapply而不是循环.即使对于带有参数的函数,例如

R textbooks continue to promote the use of lapply instead of loops. This is easy even for functions with arguments like

lapply(somelist, f, a=1, b=2) 

但是如果参数根据list元素改变怎么办? 假设我的清单包含:

but what if the arguments change depending on the list element? Assume my somelist consists of:

somelist$USA
somelist$Europe
somelist$Switzerland

加上anotherlist具有相同的区域,我想对这些变化的参数使用lapply吗?例如,当f是比率计算时,这可能很有用.

plus there is anotherlist with the same regions and I want use lapply with these changing arguments? This could be useful when f was a ratio calculation for example.

lapply(somelist, f, a= somelist$USA, b=anotherlist$USA) 

除了循环之外,还有其他方法可以有效地在这些区域中运行吗?

Is there are way except for a loop to run through these regions efficiently?

我的问题似乎是我尝试使用以前编写的没有索引的函数...

my problem seems to be that I tried to use a previously written function without indexes...

ratio <-function(a,b){
z<-(b-a)/a
return(z)
}

导致

lapply(data,ratio,names(data))

这不起作用.也许其他人也可以从这个错误中学到东西.

which does not work. Maybe others can also learn from this mistake.

推荐答案

应用于列表名称,而不是列表元素.例如:

Apply over list names rather than list elements. E.g.:

somelist <- list('USA'=rnorm(10), 'Europe'=rnorm(10), 'Switzerland'=rnorm(10))
anotherlist <- list('USA'=5, 'Europe'=10, 'Switzerland'=4)
lapply(names(somelist), function(i) somelist[[i]] / anotherlist[[i]])

您还询问是否有一种方法除了循环"才能有效"地执行此操作.您应注意,申请不一定会更有效.效率可能取决于内部功能的速度.如果要对列表的每个元素进行操作,则无论是否将其隐藏在apply()调用中,都将需要一个循环.检查以下问题: R的适用范围是否比句法糖还要多?

You also ask if there is a way "except for a loop" to do this "efficiently". You should note that the apply will not necessarily be more efficient. Efficiency will probably be determined by how quick your inner function is. If you want to operate on each elements of a list, you will need a loop, whether it is hidden in an apply() call or not. Check this question: Is R's apply family more than syntactic sugar?

我上面给出的示例可以重写为for循环,并且您可以进行一些简单的基准测试:

The example I gave above can be re-written as a for loop, and you can make some naive benchmarks:

fun1 <- function(){
    lapply(names(somelist), function(i) somelist[[i]] / anotherlist[[i]])
}
fun2 <- function(){
    for (i in names(somelist)){
        somelist[[i]] <- somelist[[i]] / anotherlist[[i]] 
    }
    return(somelist)
}
library(rbenchmark)

benchmark(fun1(), fun2(),
          columns=c("test", "replications",
          "elapsed", "relative"),
          order="relative", replications=10000)

我的计算机上基准测试的输出为:

The output of the benchmark on my machine was this:

    test replications elapsed relative
1 fun1()        10000   0.145 1.000000
2 fun2()        10000   0.148 1.020690

尽管这不是真正的工作应用程序,并且功能不是现实的任务,但是您可以看到计算时间的差异可以忽略不计.

Although this is not a real work application and the functions are not realistic tasks, you can see that the difference in computation time is quite negligible.

这篇关于在改变参数的情况下使用lapply的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆