Stock.Open <- rep(c(102.25,102.87,102.25,100.87,103.44,103.87,103.00),times=3)
Stock.Close <- rep(c(102.12,102.62,100.12,103.00,103.87,103.12,105.12), times=3)
Stock.id<-rep(1:3,each=7)
day<-rep(c(1:7),times=3)
df<-data.frame(day,Stock.Close,Stock.Open,Stock.id)
如何计算每天w.r.t到前一天stock.open和stock.close的%差异。例如:我想在day1,day2和day2,day3,day3,day4之间计算Stock.open的%变化,等等。
为每个ID执行相同的任务。
答案 0 :(得分:0)
试试这个:
Stock.Open <- rep(c(102.25,102.87,102.25,100.87,103.44,103.87,103.00),times=3)
Stock.Close <- rep(c(102.12,102.62,100.12,103.00,103.87,103.12,105.12), times=3)
Stock.id<-rep(1:3,each=7)
day<-rep(c(1:7),times=3)
df<-data.frame(day,Stock.Close,Stock.Open,Stock.id)
#Subset stock data per day
temp <- subset(df, select = c(Stock.Open, Stock.Close, day, Stock.id))
#Change day and rename
temp$day <- temp$day + 1
require(plyr)
temp <- plyr::rename(temp, c("Stock.Open" = "Stock.Open.Pre", "Stock.Close" = "Stock.Close.Pre"))
#Merge back
df <- join(df, temp, by = c("day", "Stock.id"), type = "left")
#Compute difference
df$Stock.Open.Diff <- (df$Stock.Open / df$Stock.Open.Pre) - 1
df$Stock.Close.Diff <- (df$Stock.Close / df$Stock.Close.Pre) - 1
答案 1 :(得分:0)
试试这个:
library(dplyr)
df %>%
group_by(Stock.id) %>%
arrange(day) %>%
mutate(Change.Stock.Open = c(NA, diff(Stock.Open))/Stock.Open,
Change.Stock.Close = c(NA, diff(Stock.Close))/Stock.Close)
# A tibble: 21 x 6
# Groups: Stock.id [3]
day Stock.Close Stock.Open Stock.id Change.Stock.Open Change.Stock.Close
<int> <dbl> <dbl> <int> <dbl> <dbl>
1 1 102.12 102.25 1 NA NA
2 1 102.12 102.25 2 NA NA
3 1 102.12 102.25 3 NA NA
4 2 102.62 102.87 1 0.006027024 0.004872345
5 2 102.62 102.87 2 0.006027024 0.004872345
6 2 102.62 102.87 3 0.006027024 0.004872345
7 3 100.12 102.25 1 -0.006063570 -0.024970036
8 3 100.12 102.25 2 -0.006063570 -0.024970036
9 3 100.12 102.25 3 -0.006063570 -0.024970036
10 4 103.00 100.87 1 -0.013680976 0.027961165
# ... with 11 more rows
(每个股票的第1天相关的值为NA
,因为没有前一天进行比较)
答案 2 :(得分:0)
又一种解决方案,这次仅使用基数R.
df$Stock.Close.Change <- ave(df$Stock.Close, df$Stock.id, FUN = function(x) c(NA, diff(x))/x)
df$Stock.Open.Change <- ave(df$Stock.Open, df$Stock.id, FUN = function(x) c(NA, diff(x))/x)
df <- df[order(df$day), ]
row.names(df) <- NULL
df
day Stock.Close Stock.Open Stock.id Stock.Close.Change Stock.Open.Change
1 1 102.12 102.25 1 NA NA
2 1 102.12 102.25 2 NA NA
3 1 102.12 102.25 3 NA NA
4 2 102.62 102.87 1 0.004872345 0.006027024
5 2 102.62 102.87 2 0.004872345 0.006027024
6 2 102.62 102.87 3 0.004872345 0.006027024
7 3 100.12 102.25 1 -0.024970036 -0.006063570
8 3 100.12 102.25 2 -0.024970036 -0.006063570
9 3 100.12 102.25 3 -0.024970036 -0.006063570
10 4 103.00 100.87 1 0.027961165 -0.013680976
11 4 103.00 100.87 2 0.027961165 -0.013680976
12 4 103.00 100.87 3 0.027961165 -0.013680976
13 5 103.87 103.44 1 0.008375854 0.024845321
14 5 103.87 103.44 2 0.008375854 0.024845321
15 5 103.87 103.44 3 0.008375854 0.024845321
16 6 103.12 103.87 1 -0.007273080 0.004139790
17 6 103.12 103.87 2 -0.007273080 0.004139790
18 6 103.12 103.87 3 -0.007273080 0.004139790
19 7 105.12 103.00 1 0.019025875 -0.008446602
20 7 105.12 103.00 2 0.019025875 -0.008446602
21 7 105.12 103.00 3 0.019025875 -0.008446602