Hi all,
I was wondering whether it is possible to use the lapply() function
to alter the value of the input, something in the spirit of :
a1<-runif(100)
a2<-function(i){
a1[i]<-a1[i-1]*a1[i];a1[i]
}
a3<-lapply(2:100,a2)
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.
Thanks in advance,
On Oct 13, 2009, at 7:33 AM, Kaveh Vakili wrote:> > Hi all, > > I was wondering whether it is possible to use the lapply() function > to alter the value of the input, something in the spirit of : > > a1<-runif(100) > a2<-function(i){ > a1[i]<-a1[i-1]*a1[i];a1[i] > } > a3<-lapply(2:100,a2)Neither a1 nor 2:100 are lists, so it would seem that sapply would be more appropriate.> > Something akin to a for() loop, but using the lapply() infrastructure. > I haven't been able to get rapply() to do this.You did not specify what the correct answer should look like, but I get no error after changing the "l" to an "s" and the output is a vector rather than a list. I got no error with the lapply version so it remains unclear what problem you are experiencing. > a1<-runif(100) > a2<-function(i){ + a1[i]<-a1[i-1]*a1[i];a1[i] + } > a3<-sapply(2:100,a2) > a3 [1] 2.990506e-01 2.957213e-02 3.343994e-02 7.234998e-01 2.036053e-01 1.850228e-01 2.355974e-01 3.295134e-01 [9] 3.206837e-01 1.073884e-02 1.121334e-02 1.368814e-01 1.381827e-01 3.426581e-01 3.683766e-01 2.096506e-01 snipped> > 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.Then maybe you should only apply it when those criteria are met?> > Thanks in advance, > > ______________________________________________-- David Winsemius, MD Heritage Laboratories West Hartford, CT
> Neither a1 nor 2:100 are lists, so it would seem that sapply would be more > appropriate.The difference between lapply and sapply is the output, not the input. Hadley -- http://had.co.nz/
On Tue, 13 Oct 2009, Kaveh Vakili wrote:> > Hi all, > > I was wondering whether it is possible to use the lapply() function > to alter the value of the input, something in the spirit of : > > a1<-runif(100) > a2<-function(i){ > a1[i]<-a1[i-1]*a1[i];a1[i] > } > a3<-lapply(2:100,a2) > > Something akin to a for() loop, but using the lapply() infrastructure. > I haven't been able to get rapply() to do this.Maybe you want to check out ?Reduce For the example above, something like a3 <- Reduce( "*", a1, accumulate = TRUE ) HTH, Chuck> > 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. > > Thanks in advance, > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
Thanks, this is the closest to what i want (gives the same result as cumprod())
...but using this function seems actually slower than the loop (is it normal ?):
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(looop(a1))
user system elapsed
0.772 0.000 0.775 >
>On Tue, 13 Oct 2009, Kaveh Vakili wrote:
>
>>
>> Hi all,
>>
>> I was wondering whether it is possible to use the lapply() function
>> to alter the value of the input, something in the spirit of :
>>
>> a1<-runif(100)
>> a2<-function(i){
>> a1[i]<-a1[i-1]*a1[i];a1[i]
>> }
>> a3<-lapply(2:100,a2)
>>
>> Something akin to a for() loop, but using the lapply() infrastructure.
>> I haven't been able to get rapply() to do this.
>
>Maybe you want to check out
>
> ?Reduce
>
>For the example above, something like
>
>a3 <- Reduce( "*", a1, accumulate = TRUE )
>
>
>HTH,
>
>Chuck
>
>>
>> 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.
>>
>> Thanks in advance,
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>Charles C. Berry (858) 534-2098
> Dept of Family/Preventive
Medicine
>E mailto:cberry at tajo.ucsd.edu UC San Diego
>http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
>
>
>
>
Thanks,Chuck answer is the closest to what i want (gives the same result as
cumprod()) ...but using this function seems actually slower than the loop (is it
normal ?):
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(looop(a1))
> user system elapsed
> 0.772 0.000 0.775
>>
>
>
>>On Tue, 13 Oct 2009, Kaveh Vakili wrote:
>>
>>>
>>> Hi all,
>>>
>>> I was wondering whether it is possible to use the lapply() function
>>> to alter the value of the input, something in the spirit of :
>>>
>>> a1<-runif(100)
>>> a2<-function(i){
>>> a1[i]<-a1[i-1]*a1[i];a1[i]
>>> }
>>> a3<-lapply(2:100,a2)
>>>
>>> Something akin to a for() loop, but using the lapply()
infrastructure.
>>> I haven't been able to get rapply() to do this.
>>
>>Maybe you want to check out
>>
>> ?Reduce
>>
>>For the example above, something like
>>
>>a3 <- Reduce( "*", a1, accumulate = TRUE )
>>
>>
>>HTH,
>>
>>Chuck
>>
>>>
>>> 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.
>>>
>>> Thanks in advance,
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>Charles C. Berry (858) 534-2098
>> Dept of Family/Preventive
Medicine
>>E mailto:cberry at tajo.ucsd.edu UC San Diego
>>http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego
92093-0901
>>
>>
>>
>>
>
>
>
> no attachments have been sent
>
>
Thanks, Chuck's answer is the closest to what i want (gives the same result
as cumprod()) ...but using this function seems actually slower than the loop (is
it normal ?):
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(looop(a1))
> user system elapsed
> 0.772 0.000 0.775
>>
>
>
>>On Tue, 13 Oct 2009, Kaveh Vakili wrote:
>>
>>>
>>> Hi all,
>>>
>>> I was wondering whether it is possible to use the lapply() function
>>> to alter the value of the input, something in the spirit of :
>>>
>>> a1<-runif(100)
>>> a2<-function(i){
>>> a1[i]<-a1[i-1]*a1[i];a1[i]
>>> }
>>> a3<-lapply(2:100,a2)
>>>
>>> Something akin to a for() loop, but using the lapply()
infrastructure.
>>> I haven't been able to get rapply() to do this.
>>
>>Maybe you want to check out
>>
>> ?Reduce
>>
>>For the example above, something like
>>
>>a3 <- Reduce( "*", a1, accumulate = TRUE )
>>
>>
>>HTH,
>>
>>Chuck
>>
>>>
>>> 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.
>>>
>>> Thanks in advance,
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>Charles C. Berry (858) 534-2098
>> Dept of Family/Preventive
Medicine
>>E mailto:cberry at tajo.ucsd.edu UC San Diego
>>http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego
92093-0901
>>
>>
>>
>>
>
>
>
>
Thanks, Chuck's answer is the closest to what i want (gives the same result
as cumprod()) ...but using this function seems actually slower than the loop (is
it normal ?):
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(looop(a1))
> user system elapsed
> 0.772 0.000 0.775
>>
>
>
>>On Tue, 13 Oct 2009, Kaveh Vakili wrote:
>>
>>>
>>> Hi all,
>>>
>>> I was wondering whether it is possible to use the lapply() function
>>> to alter the value of the input, something in the spirit of :
>>>
>>> a1<-runif(100)
>>> a2<-function(i){
>>> a1[i]<-a1[i-1]*a1[i];a1[i]
>>> }
>>> a3<-lapply(2:100,a2)
>>>
>>> Something akin to a for() loop, but using the lapply()
infrastructure.
>>> I haven't been able to get rapply() to do this.
>>
>>Maybe you want to check out
>>
>> ?Reduce
>>
>>For the example above, something like
>>
>>a3 <- Reduce( "*", a1, accumulate = TRUE )
>>
>>
>>HTH,
>>
>>Chuck
>>
>>>
>>> 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.
>>>
>>> Thanks in advance,
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>Charles C. Berry (858) 534-2098
>> Dept of Family/Preventive
Medicine
>>E mailto:cberry at tajo.ucsd.edu UC San Diego
>>http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego
92093-0901
>>
>>
>>
>>
>
>
>
>