[R] Relative subscripting

Tobias Muhlhofer t.muhlhofer at lse.ac.uk
Tue Nov 30 22:50:01 CET 2004


OK, yeah. I did think of that in the meantime, but I was also wondering 
if there was some other convenience function to do it. But this will do 
in any case.

Toby


Jean Eid wrote:
>  wouldn't it be return[2:NROW(return)]*marketcap[1:(NROW(return)-1)]
> 
> (note that return is also a function in R so maybe you should stay away
> from calling your variable return. Anyways if you have a data frame you
> can add another variable to it but attach an NA to the first element
> (since you are lagging variables) i.e.
> 
> mydata$myvar<-c(NA, mydata$return[2:nrow(mydata)]*mydata$marketcap[1:(nrow(mydata)-1)]
> 
> 
> Jean
> 
> 
> 
> On Tue, 30 Nov 2004, Tobias Muhlhofer wrote:
> 
> 
>>Hi!
>>
>>I'm trying to do the following.
>>
>>I have monthly a dataset with, among other things, "marketcap", and
>>"return".
>>
>>I want to multiply return by the marketcap of the previous month. How do
>>I do this?
>>
>>In STATA (which I have used frequently in the past) I would simply use
>>an expression of the form
>>
>>return[_n]*marketcap[_n-1]
>>
>>I believe they call this relative subscripting. Is there something like
>>this in R?
>>
>>On a completely different note, are there books on R? I read the
>>"Getting Started" notes, but, while helpful, I would need more than those.
>>
>>Thanks,
>>	Toby
>>
>>______________________________________________
>>R-help at stat.math.ethz.ch mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>>
> 
> 
> 

-- 
**************************************************************************
When Thomas Edison invented the light bulb he tried over 2000
experiments before he got it to work. A young reporter asked
him how it felt to have failed so many times. He said
"I never failed once. I invented the light bulb.
It just happened to be a 2000-step process."




More information about the R-help mailing list