我的数据集如下所示:
symbol date adjusted
BAC 2000-01-03 13.61120
BAC 2000-01-04 12.80331
BAC 2000-01-05 12.94381
BAC 2000-01-06 14.05027
BAC 2000-01-07 13.68145
BAC 2000-01-10 13.20725
在符号下面,有三种不同的股票。我想添加一个关于资产每日回报的专栏,但我一直在做什么。
假设每日回报是前一天和当前一天之间的差额,这应该会给你想要的:
library(dplyr)
df %>%
group_by(symbol) %>%
mutate(return = adjusted - lag(adjusted, 1))
# A tibble: 6 × 4
# Groups: symbol [1]
symbol date adjusted return
<chr> <date> <dbl> <dbl>
1 BAC 2000-01-03 13.6 NA
2 BAC 2000-01-04 12.8 -0.808
3 BAC 2000-01-05 12.9 0.140
4 BAC 2000-01-06 14.1 1.11
5 BAC 2000-01-07 13.7 -0.369
6 BAC 2000-01-10 13.2 -0.474
模拟具有多个symbol
值的数据;然后,正如Just James的评论所建议的,使用tapply
将diff
函数分别应用于每组。您需要在开始时添加一个NA,因为您无法计算第一个值的回报。
df <- structure(list(symbol = c("BAC", "BAC", "BAC", "BAC", "BAC",
"BAC", "CAB", "CAB", "CAB", "CAB", "CAB", "CAB", "ACB", "ACB",
"ACB", "ACB", "ACB", "ACB"), date = c("2000-01-03", "2000-01-04",
"2000-01-05", "2000-01-06", "2000-01-07", "2000-01-10", "2000-01-03",
"2000-01-04", "2000-01-05", "2000-01-06", "2000-01-07", "2000-01-10",
"2000-01-03", "2000-01-04", "2000-01-05", "2000-01-06", "2000-01-07",
"2000-01-10"), adjusted = c(13.6112, 12.80331, 12.94381, 14.05027,
13.68145, 13.20725, 13.6112, 12.80331, 12.94381, 14.05027, 13.68145,
13.20725, 13.6112, 12.80331, 12.94381, 14.05027, 13.68145, 13.20725
)), class = "data.frame", row.names = c(NA, -18L))
df$returns <- unlist(tapply(df$adjusted, df$symbol, function(adj) c(NA, diff(adj))))
df
symbol date adjusted returns
1 BAC 2000-01-03 13.61120 NA
2 BAC 2000-01-04 12.80331 -0.80789
3 BAC 2000-01-05 12.94381 0.14050
4 BAC 2000-01-06 14.05027 1.10646
5 BAC 2000-01-07 13.68145 -0.36882
6 BAC 2000-01-10 13.20725 -0.47420
7 CAB 2000-01-03 13.61120 NA
8 CAB 2000-01-04 12.80331 -0.80789
9 CAB 2000-01-05 12.94381 0.14050
10 CAB 2000-01-06 14.05027 1.10646
11 CAB 2000-01-07 13.68145 -0.36882
12 CAB 2000-01-10 13.20725 -0.47420
13 ACB 2000-01-03 13.61120 NA
14 ACB 2000-01-04 12.80331 -0.80789
15 ACB 2000-01-05 12.94381 0.14050
16 ACB 2000-01-06 14.05027 1.10646
17 ACB 2000-01-07 13.68145 -0.36882
18 ACB 2000-01-10 13.20725 -0.47420