[R] Relative subscripting
Jean Eid
jeaneid at chass.utoronto.ca
Tue Nov 30 22:51:13 CET 2004
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
>
More information about the R-help
mailing list