[R] How to apply apply?!

Abhijit Dasgupta, PhD aikidasgupta at gmail.com
Fri Aug 6 21:13:52 CEST 2010


For 1, an easy way is

dat <- transform(dat, CLOSE2=2*CLOSE)

For 2:

apply(dat,1,fun)

On 08/06/2010 03:06 PM, Raghuraman Ramachandran wrote:
> guRus
>
> I have say a dataframe, d and I wish to do the following:
>
> 1) For each row, I want to take one particular value of the row and multiply
> it by 2. How do I do it. Say the data frame is as below:
>     OPEN HIGH LOW CLOSE 1931.2 1931.2 1931.2 1931.2 0 0 0 999.05 0 0 0 1052.5
> 0 0 0 987.8 0 0 0 925.6 0 0 0 866 0 0 0 1400.2 0 0 0 754.5 0 0 0 702.6 0 0 0
> 653.25 0 0 0 348 0 0 0 801 866.55 866.55 866.55 866.55 783.1 783.1 742.25
> 742.25 575 575 575 575 0 0 0 493 470 470 420 425 355 360 343 360 312.05
> 312.05 274 280.85 257.35 257.35 197 198.75 182 185.95 137 150.75 120.25 129
> 90.7 101.25 91.85 91.85 57 66.6
>
> How do I multiply only the close of every row using the 'apply' function?
> And once multiplied how do I obtain a new table that also contains the new
> 2*CLOSE column (without cbind?).
>
> 2) Also, how do I run a generic function per row. Say for example I want to
> calculate the Implied Volatility for each row of this data frame ( using the
> RMterics package). How do I do that please using the apply function? I am
> focusing on apply because I like the vectorisation concept in R and I do not
> want to use a for loop etc.
>
> Many thanks for the enlightment,
> Raghu
>
> 	[[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.
>    


-- 

Abhijit Dasgupta, PhD
Director and Principal Statistician
ARAASTAT
Ph: 301.385.3067
E: adasgupta at araastat.com
W: http://www.araastat.com



More information about the R-help mailing list