您可以使用 lapply() 函数来更改输入的值吗?

发布于 2024-08-08 02:01:51 字数 1456 浏览 4 评论 0原文

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

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

我正在寻找类似于 for() 循环的东西,但使用 lapply() 基础设施。我无法让 rapply() 来执行此操作。

原因是“真正的”a2 函数是一个困难的函数,仅当 a1[i-1] 的值满足某些条件时才需要对其进行评估。

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

    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() 函数......如

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

:与 cumprod(a1) 的结果相同...但甚至比循环慢:

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 
> 

有什么想法吗?

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)

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.

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.

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))

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)

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 
> 

Any idea ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软糯酥胸 2024-08-15 02:01:51

编辑:根据您的澄清:不,我不相信您可以使用 apply 函数来递归地执行类似的操作。 apply 函数的重点在于它同时应用于向量/矩阵。

您可能还想查看 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中所需的任何内容的引用。返回也是一个列表,因此您需要将其转换回您想要的任何形式。

您可能还想查看 plyr 包以获取完成此类操作的简单方法。

除此之外,您可以在没有循环的情况下执行操作:

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

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

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

但第一个版本更可取,因为它没有迭代。

Edit: 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.

You may also want to look at this related question on stackoverflow.

My old answer:

Try this:

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))

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文