[R] Simplifying particular piece of code
Sergey Goriatchev
sergeyg at gmail.com
Wed Mar 31 12:10:50 CEST 2010
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.
More information about the R-help
mailing list