需要计算两个数据集随时间的变化率和净变动率 [英] Need to calculate Rate of Change of two data sets over time individually and Net rate of Change
问题描述
我到目前为止做了什么:
read.csv(filename.csv,header = TRUE)
df $ header1
df $ header2
现在我想计算变化率:Header1是Dates,Header2是价格
所有值与之前日期相比的日期变化率。
我想生成两个单独的更改对另一个文件执行相同的操作。
一旦两个数据集都有更改率,我将查找每个日期的净更改率。
我现在的位置:
df< -read.csv (audusd.csv,head = TRUE)
df $ Date
df $ Close
rate < - 100 * diff(df $ Close / df [-nrow ),] $ Close
这样执行,然后得到:
> rate< - 100 * diff(df $ Close / df [-nrow(df),] $ Close
+
+
在控制台中。
所有的帮助到现在,请帮助进一步。 :)
此外,我有点困惑,我是否可以将结果导入文件?或者每次我必须运行代码吗?
如果我能理解并开始使用它,这肯定有助于我的工作。
您也可以使用 diff(...)
您可以避免在各行之间循环:
rate< - 100 * diff(df $ price)/ df [-nrow ),] $ price
diff(...)
计算差分行(n) - 行(n-1)。 df [-nrow(df),]
返回除最后一行之外的 df
的所有行。
$ b
这是一个有效代码的完整示例。
#这只是为了获取示例数据。
每日关闭AAPL从2013.01.01 - 今天
库(tseries)
库(动物园)
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 (df)),price = df $ AdjClose)
df < - df [!is.na(df $ price),]
#计算每日变化率...
rate <-100 * diff(df $ price)/ df [-nrow(df),] $ price
plot(df [-nrow(df),] $ date,rate ,type =l,xlab =2013,ylab =Pct。Change,main =APPL)
b $ b
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.
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")
这篇关于需要计算两个数据集随时间的变化率和净变动率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!