1

What I have done so far:

read.csv("filename.csv", header=TRUE)
df$header1
df$header2

Now I want to calculate the Rate of Change: Header1 is Dates, Header2 is prices Rate of Change by date for all values comparative to preceding date.

I want to generate two separate columns of Rate of Change performing the same operation on another file.

Once rate of change is available for both the data sets, I will look to find the net rate of change for each date.

Where I am now:

df<-read.csv("audusd.csv", head = TRUE)
df$Date
df$Close

rate<- 100*diff(df$Close/df[-nrow(df),]$Close

This executes and then I get this:

> rate<- 100*diff(df$Close/df[-nrow(df),]$Close
+ 
+ 

In the console.

Thanks for all the help till now, please help further. :)

Also, I am a bit confused about whether I can get the results into a file? Or Do I have to run the code each time?

This certainly would help with my work in a big way, if I can understand and start using it.

Sid
  • 35
  • 1
  • 6

2 Answers2

4

You can also use the diff(...) function, which allows you to avoid looping through rows:

rate <- 100*diff(df$price)/df[-nrow(df),]$price

diff(...) calculates the difference row(n) - row(n-1). df[-nrow(df),] returns all rows of df except the last.

Here's a complete example with working code.

# all this just to get sample data.
# daily close for AAPL from 2013.01.01 - today
library(tseries)
library(zoo)
ts <- get.hist.quote(instrument="AAPL", 
                     start="2013-01-01", end="2014-01-01", 
                     quote="AdjClose", provider="yahoo", origin="1970-01-01",
                     compression="d", retclass="zoo")
df <- data.frame(ts)
df <- data.frame(date=as.Date(rownames(df)),price=df$AdjClose)
df <- df[!is.na(df$price),]

# calculate daily rate of change...
rate <- 100*diff(df$price)/df[-nrow(df),]$price

plot(df[-nrow(df),]$date,rate,type="l",xlab="2013",ylab="Pct. Change",main="APPL")

jlhoward
  • 58,004
  • 7
  • 97
  • 140
  • Note the `arithmetic` argment on `diff.zoo`. – G. Grothendieck Dec 22 '13 at 03:55
  • Thanks. Getting error now when tyring to use the commands in the opening post. The error: Error in df$Date : object of type 'closure' is not subsettable. It reads the CSV but setting the dataframe is somehow giving me that error. Back to the books. – Sid Dec 22 '13 at 10:04
  • Just to be clear, the first 5 lines of code were just to st up the example. You already have a data frame. – jlhoward Dec 22 '13 at 11:36
  • Hi, I dont know whats happening with it. I am using the exact code as before but this time it keeps giving me the "object of type "closure" is not subsettable. Tried using it with just the read.csv() command, reads the file and then when I try the df$Date it simply gives me the error. The first column on display of csv is the column numbers without any header so I just go for the commands used earlier. Confused – Sid Dec 22 '13 at 18:23
  • @user3126171 - Can you cut and paste your exact code and the error message into your question as an edit? – jlhoward Dec 22 '13 at 18:26
  • Hi, Thanks. I wasn't using the df=read.csv() Got it to work now. – Sid Dec 27 '13 at 02:17
0

Given what you said in the comments about how you would do it in excel here is the R equivalent.

dataset=(1:10)^3 #Data in your case one of your columns
ratedata=NULL # Something you have to do so R can put data in this object
for(i in 1:(length(dataset)-1)){ratedata[i]=(dataset[i+1]-dataset[i])/dataset[i]*100} # A simple for function doing what you specified. 
ratedata # Returning the rate data so you can look at it

FYI:This only works if your time is at regular intervals.

CCurtis
  • 1,770
  • 3
  • 15
  • 25
  • Thanks. Getting error now when tyring to use the commands in the opening post. The error: Error in df$Date : object of type 'closure' is not subsettable. It reads the CSV but setting the dataframe is somehow giving me that error. Back to the books. – Sid Dec 22 '13 at 10:03
  • It sounds like your not sure how to load your data. In that case I would recommend using one of the GUIs for R if you're not already doing so. I use Rstudio but there are others. In Rstudio you can load data sort of like excel where you can preview the data before you load it. You can also look at the data while you are working with it. Once you load your data right you can use `dataset=df[,2]` where df is you dataset. then run the rest of my code. – CCurtis Dec 23 '13 at 01:18
  • Does your dataset have headers? If so `df=read.csv("filename.csv", header=TRUE)`. If not `df=read.csv("filename.csv", header=FALSE)`. Make sure you are setting your data to an object i.e. `df=read.csv(...`. If you say `read.csv(...` R will just display your data. – CCurtis Dec 23 '13 at 01:23
  • Hi Thanks, Used the df=read.csv("filename.csv",header=TRUE) command and managed to get the data points sorted. :) – Sid Dec 27 '13 at 02:19
  • About the code that you have given above, the data I am working with is at regular intervals but missing in a few places which I have deleted. So it does jump around in certain places. Weekends for example are always excluded. Is the 1:10 part an indication of the number of rows of data available or? – Sid Dec 27 '13 at 02:34