Skip to content
Prev 299160 / 398506 Next

Return

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.