[R] Simplifying particular piece of code

Sergey Goriatchev sergeyg at gmail.com
Wed Mar 31 17:37:03 CEST 2010


Thanks for help, Gustaf!

(Kan man säga "man tackar" oxa?) :-)

On Wed, Mar 31, 2010 at 17:34, Gustaf Rydevik <gustaf.rydevik at gmail.com> wrote:
> On Wed, Mar 31, 2010 at 5:11 PM, Sergey Goriatchev <sergeyg at gmail.com> wrote:
>> but
>>
>> data <- merge(data,data.list)
>>
>> works.
>>
>> Neither data or data.list is a list, so do.call does not work.
>> I am very weak on lists, never used them before
>>
>> Best,
>> Sergey
>
> Hej Sergey,
>
> Ok; I was wondering if the apply thing would work. Cool that merge
> would be clever enough to append a matrix. I'm guessing that you've
> got what you needed then? For reference,  (and for the general list) I
> had changed the  code  before Sergeys response, replacing apply() with
> lapply(). That code follows below.
> Best regards,
> Gustaf
>
>
> -----------------------------
> cnames <- c("BMM", "GM1", "IYC", "FCA", "IMM", "BME", "CRT", "GTF",
> "ERU", "ERE", "EPT", "EVA", "EMT", "EMM", "EMV", "ETM")
> AVnames <- paste(cnames, ".AV120", sep="")
> SDnames <- paste(cnames, ".SD120", sep="")
> a <- zoo(matrix(rep(seq(from=160, to=10, by=-10), 1000), ncol=16, byrow=TRUE))
> colnames(a) <- AVnames
> b <- zoo(matrix(rep(2, 16000), ncol=16))
> colnames(b) <- SDnames
> data <- merge(a, b)
> MyFunc <- function(x, ret, stdev){
>       if(any(is.na(c(x[ret], x[stdev])))){
>               return(NA)
>       }else{
>               return(x[ret]/x[stdev])
>       }
> }
> names.df<-data.frame(rbind(SRnames,AVnames,SDnames))
> func <- function(.names){
>       apply(data, 1, MyFunc, ret=.names[2], stdev=.names[3])
> }
> data.list<-lapply(names.df, func)
> mrets<-do.call("merge",c(list(data),data.list))
>
>
>
>
>
>
>
>
>
> On Wed, Mar 31, 2010 at 12:33, Gustaf Rydevik <gustaf.rydevik at gmail.com> wrote:
>> 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
>



-- 
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