我正在尝试在我的DF中创建一个名为"Returns"的新列,但是我很难找到如何在同一列中划分2个变量,但在不同的行中。
以下是DF:
Date Open High Low AdjClose
2014-02-13 2014-02-13 1815 1830 1809 1830
2014-02-12 2014-02-12 1820 1827 1816 1819
2014-02-11 2014-02-11 1800 1824 1800 1820
2014-02-10 2014-02-10 1796 1800 1792 1800
2014-02-07 2014-02-07 1776 1798 1776 1797
2014-02-06 2014-02-06 1753 1774 1753 1773
我假设代码看起来像这样:
SPXprices.df$Returns <- (SPXprices.df$AdjClose - SPXprices$AdjClose[x,x])/SPXprices$AdjClose[x,x]
我希望有人能在这里帮助我。
任何见解将不胜感激!
这是一种通过参考前一天来做到这一点的方法
returns<-sapply(1:length(SPXprices.df$AdjClose),
FUN=function(x) (SPXprices.df$AdjClose[x] - SPXprices.df$AdjClose[x+1])/SPXprices.df$AdjClose[x+1])
SPXprices.df$Returns <- returns
在sapply
函数中,我使用SPXprices.df$AdjClose[x+1])
引用第二天
这是你要找的吗?
SPXprices.df <- read.table(header=T, text=
"
Date Open High Low AdjClose
2014-02-13 2014-02-13 1815 1830 1809 1830
2014-02-12 2014-02-12 1820 1827 1816 1819
2014-02-11 2014-02-11 1800 1824 1800 1820
2014-02-10 2014-02-10 1796 1800 1792 1800
2014-02-07 2014-02-07 1776 1798 1776 1797
2014-02-06 2014-02-06 1753 1774 1753 1773
")
#sorting the data
SPXprices.df <- SPXprices.df[order(SPXprices.df$Date),]
#using lagpad from http://stackoverflow.com/a/13128713/2862090 to lag
lagpad <- function(x, k) {
c(rep(NA, k), x)[1 : length(x)]
}
#showing the result of lagpad
SPXprices.df$lagAdjClose <- lagpad(SPXprices.df$AdjClose,1)
#calculate returns
SPXprices.df$Returns <- (SPXprices.df$AdjClose - lagpad(SPXprices.df$AdjClose,1))/lagpad(SPXprices.df$AdjClose,1)
SPXprices.df
Date Open High Low AdjClose lagAdjClose Returns
2014-02-06 2014-02-06 1753 1774 1753 1773 NA NA
2014-02-07 2014-02-07 1776 1798 1776 1797 1773 0.0135363790
2014-02-10 2014-02-10 1796 1800 1792 1800 1797 0.0016694491
2014-02-11 2014-02-11 1800 1824 1800 1820 1800 0.0111111111
2014-02-12 2014-02-12 1820 1827 1816 1819 1820 -0.0005494505
2014-02-13 2014-02-13 1815 1830 1809 1830 1819 0.0060472787