您可以使用lapply()函数更改输入的值吗? [英] Can you use the lapply() function to alter the value of input?

查看:84
本文介绍了您可以使用lapply()函数更改输入的值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用lapply()函数来更改输入的值,类似于:

I was wondering whether it is possible to use the lapply() function to alter the value of the input, similar to:

a1<-runif(100)
a2<-function(i){
a1[i]<-a1[i-1]*a1[i];a1[i]
}
a3<-lapply(2:100,a2)

我正在寻找类似于for()循环的东西,但是使用了lapply()基础结构.我无法通过rapply()来做到这一点.

I'm looking for something akin to a for() loop, but using the lapply() infrastructure. I haven't been able to get rapply() to do this.

原因是实数" a2函数是一个困难的函数,只有在a1 [i-1]的值满足某些条件时才需要对其进行评估.

The reason is that the "real" a2 function is a difficult function that only needs to be evaluated if the value of a1[i-1] meets some criteria.

重新措辞:所以我试图用lapply()类型的东西替换下面代码中的for():

re-phrasing: so i'm trying to replace the for() in the code below by a lapply()-type thing:

    a1<-runif(100)
    a2<-function(i, a1){
        a1[i]<-a1[i-1]*2
        a1[i]
    }
    a3<-as.numeric(lapply(2:100, a2, a1=a1))
#compare the output of a3 with that of a1 after the recursive loop
    a2<-a1 #saved for comparison
    for(i in 2:length(a1)){
        a1[i]<-a1[i-1]*2
    }
cbind(a1[2:100],a3)
#actually this is would be like writting a lapply() version of the cumprod() function
cbind(a1,cumprod(a2))

R邮件列表建议您查看Reduce()函数....如:

The R mailing list has advised looking unto the Reduce() function....as in:

a1<-runif(100)
cadd<-function(x) Reduce("*", x, accumulate = TRUE)
cadd(a1)

给出与cumprod(a1)相同的结果...但比循环还要慢:

which gives the same result as cumprod(a1)...but is even slower than the loop:

a1<-runif(100000)
cadd<-function(x) Reduce("*", x, accumulate = TRUE)
looop<-function(a1){
j<-length(a1)
    for(i in 2:j){
        a1[i]<-a1[i-1]*a1[i]
    }
a1
}

> system.time(cadd(a1))
   user  system elapsed 
  1.344   0.004   1.353 
> system.time(cumprod(a1))
   user  system elapsed 
  0.004   0.000   0.002 
> system.time(loop(a1))
   user  system elapsed 
  0.772   0.000   0.775 
> 

有什么主意吗?

推荐答案

在您澄清之后:不,我不认为您可以使用apply函数来递归地执行这样的操作. apply函数的全部要点是它可以同时应用于向量/矩阵.

Following your clarification: no, I don't believe that you can use an apply function to do something recursively like that. The whole point of an apply function is that it applies across the vector/matrix at the same time.

您可能还想查看有关stackoverflow的相关问题.

我的旧答案:

尝试一下:

a1<-runif(100)
a2<-function(i, a1){
    a1[i]<-a1[i-1]*a1[i]
    a1[i]
}
a3 <- as.numeric(lapply(2:100, a2, a1=a1))

for循环不同,您需要传递对lapply中所需内容的引用.返回值也是一个列表,因此您需要将其转换回所需的任何形式.

Unlike a for loop, you need to pass in a reference to anything that you need within an lapply. The return is also a list, so you need to cast it back into whatever form you want.

您可能还希望查看plyr软件包,以了解实现此类操作的简便方法.

You might also want to look at the plyr package for easy ways to do this kind of thing.

除此之外,您还可以无循环地进行操作:

Beyond that, you can do your operation without a loop:

a3 <- a1[-length(a1)] * a1[-1]

换句话说,这些语句是完全等效的:

In other words, these statements are completely equivalent:

> all((a1[-length(a1)] * a1[-1]) == as.numeric(lapply(2:100, a2, a1=a1)))
[1] TRUE

但是第一个版本是可取的,因为它没有迭代.

But the first version is preferable since it has no iterations.

这篇关于您可以使用lapply()函数更改输入的值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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