[R] Simplifying particular piece of code
Gustaf Rydevik
gustaf.rydevik at gmail.com
Wed Mar 31 12:33:46 CEST 2010
How about this (not tested, since you did not provide example data nor
function code):
---------------------------------------
SRnames <- paste(colnames.mrets, ".SR", sep="")
AVnames <- paste(colnames.mrets, ".AV120", sep="")
SDnames <- paste(colnames.mrets, ".SD120", sep="")
names.matrix<-cbind(SRnames,AVnames,SDnames)
mrets.list<-apply(names.matrix,1,function(.names){
apply(mrets,1,MyFunc,ret=.names[2],stdev=.names[3]}
)
names(mrets.list)<-names.matrix[,1]
mrets<-do.call("merge",mrets.list)
-------------------------------------------------
?
/Gustaf
On Wed, Mar 31, 2010 at 12:10 PM, Sergey Goriatchev <sergeyg at gmail.com> wrote:
> Hello, everyone
>
> I have a piece of code that looks like this:
>
> mrets <- merge(mrets, BMM.SR=apply(mrets, 1, MyFunc, ret="BMM.AV120",
> stdev="BMM.SD120"))
> mrets <- merge(mrets, GM1.SR=apply(mrets, 1, MyFunc, ret="GM1.AV120",
> stdev="GM1.SD120"))
> mrets <- merge(mrets, IYC.SR=apply(mrets, 1, MyFunc, ret="IYC.AV120",
> stdev="IYC.SD120"))
> mrets <- merge(mrets, FCA.SR=apply(mrets, 1, MyFunc, ret="FCA.AV120",
> stdev="FCA.SD120"))
> mrets <- merge(mrets, IMM.SR=apply(mrets, 1, MyFunc, ret="IMM.AV120",
> stdev="IMM.SD120"))
> mrets <- merge(mrets, BME.SR=apply(mrets, 1, MyFunc, ret="BME.AV120",
> stdev="BME.SD120"))
> mrets <- merge(mrets, CRT.SR=apply(mrets, 1, MyFunc, ret="CRT.AV120",
> stdev="CRT.SD120"))
> mrets <- merge(mrets, GTF.SR=apply(mrets, 1, MyFunc, ret="GTF.AV120",
> stdev="GTF.SD120"))
> mrets <- merge(mrets, ERU.SR=apply(mrets, 1, MyFunc, ret="ERU.AV120",
> stdev="ERU.SD120"))
> mrets <- merge(mrets, ERE.SR=apply(mrets, 1, MyFunc, ret="ERE.AV120",
> stdev="ERE.SD120"))
> mrets <- merge(mrets, EPT.SR=apply(mrets, 1, MyFunc, ret="EPT.AV120",
> stdev="EPT.SD120"))
> mrets <- merge(mrets, EVA.SR=apply(mrets, 1, MyFunc, ret="EVA.AV120",
> stdev="EVA.SD120"))
> mrets <- merge(mrets, EMT.SR=apply(mrets, 1, MyFunc, ret="EMT.AV120",
> stdev="EMT.SD120"))
> mrets <- merge(mrets, EMM.SR=apply(mrets, 1, MyFunc, ret="EMM.AV120",
> stdev="EMM.SD120"))
> mrets <- merge(mrets, EMV.SR=apply(mrets, 1, MyFunc, ret="EMV.AV120",
> stdev="EMV.SD120"))
> mrets <- merge(mrets, ETM.SR=apply(mrets, 1, MyFunc, ret="ETM.AV120",
> stdev="ETM.SD120"))
>
> Is there a way to simplify this, some sort of loop?
> mrets is a zoo object.
> .AV120 and .SD120 are columns in this object.
> I need the exact .SR column names.
>
> This does not work:
> SRnames <- paste(colnames.mrets, ".SR", sep="")
> AVnames <- paste(colnames.mrets, ".AV120", sep="")
> SDnames <- paste(colnames.mrets, ".SD120", sep="")
>
> for(i in seq(SRnames)){
> mrets <- merge(mrets, SRnames[i]=apply(mrets, 1, MyFunc,
> ret=AVnames[i], stdev=SDnames[i]))
> }
>
>
> Help much appreciated.
>
> Regards,
> Sergey
>
>
> --
> Simplicity is the last step of art./Bruce Lee
> The more you know, the more you know you don't know. /Myself
>
> I'm not young enough to know everything. /Oscar Wilde
> Experience is one thing you can't get for nothing. /Oscar Wilde
> When you are finished changing, you're finished. /Benjamin Franklin
> Luck is where preparation meets opportunity. /George Patten
>
> Kniven skärpes bara mot stenen.
>
> ______________________________________________
> 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.
>
--
Gustaf Rydevik, M.Sci.
tel: +46(0)703 051 451
address:Essingetorget 40,112 66 Stockholm, SE
skype:gustaf_rydevik
More information about the R-help
mailing list