[R] simplifying code

David Winsemius dwinsemius at comcast.net
Mon Dec 17 17:12:06 CET 2012


On Dec 17, 2012, at 5:15 AM, Rui Barradas wrote:

> Hello,
>
> Try the following.
>
> cp <- sapply(1:4, function(n) output0*exp(-0.3*TIME1[n]))
> plot(TIME1, cp)
>

That appears to be more complex than necessary. Even R-newbs should  
use vectorized approaches if they are easily available:


  cp <- output0*exp(-0.3*TIME1)

# If names are desired for those elements:

  names(cp) <- paste0("cp", 1:4)

--
>
> Hope this helps,
>
> Rui Barradas
> Em 17-12-2012 13:04, Andras Farkas escreveu:
>> Dear All,
>>
>> I was wondering if you could help me with the following:
>> I have the code:
>>
>> tin <-0.5
>> tau <-24
>> output0 <-10
>> TIMELOW <-tin
>> TIMEHIGH <-1*tau
>> TIME1 <-c(seq(TIMELOW,TIMEHIGH, by = sign(TIMEHIGH-TIMELOW)*(tau- 
>> tin)/3))
>>
>> then I would like to calculate:
>>
>> cp1 <-output0*exp(-0.3*TIME1[1])
>> cp2 <-output0*exp(-0.3*TIME1[2])
>> cp3 <-output0*exp(-0.3*TIME1[3])
>> cp4 <-output0*exp(-0.3*TIME1[4])
>>
>> then
>> plot(TIME1,c(cp1,cp2,cp3,cp4))
>>
>> is there a way to rewrite the equations for calculating the cp(1 to  
>> n) in a "single line" so that the run of the code will produce the  
>> vector  c(cp1,cp2,cp3,cp4)? At the end, I would like to have those  
>> values calculated "at once" as opposed to using a new command line  
>> to incorporate the different values for TIME1...
>>
>> thanks,
>>
>> Andras
>> 	[[alternative HTML version deleted]]
>>
>>
>>
>> ______________________________________________
>> 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.
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.

David Winsemius, MD
Alameda, CA, USA




More information about the R-help mailing list