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
Relative subscripting
5 messages · Tobias Muhlhofer, (Ted Harding), Kjetil Halvorsen +1 more
On 30-Nov-04 Tobias Muhlhofer wrote:
[...] 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?
Not as such, as far as I know. But there's an easy way to achieve
the same effect:
Let
N<-length(return)
new.var <- return[2:N]*marketcap[1:(N-1)]
Notes:
1. Note the parantheses in "1:(N-1)".
> 1:(6-1)
[1] 1 2 3 4 5
> 1:6-1
[1] 0 1 2 3 4 5
(i.e. ":" is evaluated before "-")
2. You could also define n <- 2:(N-1) in which case you could
definitely write
return[n]*marketcap[n-1]
which looks very similar to what you wrote, but I don't
know whether it implies the same underlying mechanism.
3. I'm not sure you should use "return" as the name of a variable,
since it's a predefined function in R, which you can put in a
function definition to tell it what to return:
> sq<-function(x){return(x^2)}
> sq(4)
[1] 16
In my experiments, it didn't seem to change this behaviour
to assign something to "return", e.g. return<-2, even inside
the function definition; but I'd recommend avoiding the practice!
You could avoid it here by using "Return" instead of "return"
for the name of your variable.
Best wishes,
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861 [NB: New number!]
Date: 30-Nov-04 Time: 21:34:02
------------------------------ XFMail ------------------------------
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.
Q 2.7 in the R faq (yes, there are many)! Kjetil
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
Kjetil Halvorsen.
Peace is the most effective weapon of mass construction.
-- Mahdi Elmandjra
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."
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