Hello Every one I have data on Stock prices and I want to calculate the return on all the stocks and then replace all the stock prices with the returns can any one tell me how to do My data is in the format given below Date Stock1 Stock2 Stock3 01/01/2000 1 2 3 01/02/2000 5 6 7 01/03/2000 1 2 3 01/04/2000 5 6 7 Thanks [[alternative HTML version deleted]]
Perhaps something along the lines of (untested) t(apply(yourdata[, -1], 2, function(x) diff(log(x)))) is what you are looking for. See ?apply, ?diff and ?t for more information. HTH, Jorge.- On Thu, Jul 5, 2012 at 12:58 AM, Akhil dua <> wrote:> Hello Every one > I have data on Stock prices and I want to calculate the return on all the > stocks > and then replace all the stock prices with the returns > > can any one tell me how to do > > My data is in the format given below > > Date Stock1 Stock2 Stock3 > 01/01/2000 1 2 3 > 01/02/2000 5 6 7 > 01/03/2000 1 2 3 > 01/04/2000 5 6 7 > > > Thanks > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Hi, I guess this is what you are looking for: ?dat1 <- read.table(text=" ?Date????????????? Stock1? Stock2? Stock3??? Market ?01/01/2000??????? 1????????? 2????????? 3??????????? 4 ?01/02/2000??????? 5????????? 6????????? 7??????????? 8 ?01/03/2000??????? 1????????? 2????????? 3??????????? 4 ?01/04/2000??????? 5????????? 6????????? 7??????????? 8 ?", header=TRUE, stringsAsFactors=FALSE) dat2<-data.frame(Date=as.Date(dat1[,1],format="%m/%d/%Y"),dat1[,2:4]) library(zoo) Stocks<-as.matrix(dat1[,2:4]) Stockreturn<-zoo(Stocks) Stockreturn<-diff(log(Stockreturn)) Stockreturn<-data.frame(Stockreturn) Stockreturn1<-Stockreturn ?Stockreturn1[1,]<-NA colnames(Stockreturn1[1,])<-colnames(Stockreturn) dat3<-data.frame(Date=dat2[,1], rbind(Stockreturn1[1,],Stockreturn)) row.names(dat3)<-1:nrow(dat3) ?dat3 ??????? Date??? Stock1??? Stock2???? Stock3 1 2000-01-01??????? NA??????? NA???????? NA 2 2000-01-02? 1.609438? 1.098612? 0.8472979 3 2000-01-03 -1.609438 -1.098612 -0.8472979 4 2000-01-04? 1.609438? 1.098612? 0.8472979 A.K. ----- Original Message ----- From: Akhil dua <akhil.dua.12 at gmail.com> To: r-help at r-project.org Cc: Sent: Thursday, July 5, 2012 12:58 AM Subject: [R] Return Hello Every one I have data on Stock prices and I want to calculate the return on all the stocks and then replace all the stock prices with the returns can any one tell me how to do My data is in the format given below Date? ? ? ? ? ? ? Stock1? Stock2? Stock3 01/01/2000? ? ? ? 1? ? ? ? ? 2? ? ? ? ? 3 01/02/2000? ? ? ? 5? ? ? ? ? 6? ? ? ? ? 7 01/03/2000? ? ? ? 1? ? ? ? ? 2? ? ? ? ? 3 01/04/2000? ? ? ? 5? ? ? ? ? 6? ? ? ? ? 7 Thanks ??? [[alternative HTML version deleted]] ______________________________________________ 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.
It depends on which return you want. See the "R computation" section of "A tale of two returns". http://www.portfolioprobe.com/2010/10/04/a-tale-of-two-returns/ Pat On 05/07/2012 05:58, Akhil dua wrote:> Hello Every one > I have data on Stock prices and I want to calculate the return on all the > stocks > and then replace all the stock prices with the returns > > can any one tell me how to do > > My data is in the format given below > > Date Stock1 Stock2 Stock3 > 01/01/2000 1 2 3 > 01/02/2000 5 6 7 > 01/03/2000 1 2 3 > 01/04/2000 5 6 7 > > > Thanks > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Patrick Burns pburns at pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno')