Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 从每日回报中查找每周回报公司_R_Date_Dataframe_Dplyr_Xts - Fatal编程技术网

R 从每日回报中查找每周回报公司

R 从每日回报中查找每周回报公司,r,date,dataframe,dplyr,xts,R,Date,Dataframe,Dplyr,Xts,我有这样的数据 co_code company_name co_stkdate dailylogreturn 1 A 01-01-2000 0.76 1 A 02-01-2000 0.75 . . . 1 A 31-12-2019 0.54 2 B 01-01-2000 0.98 2 B 02-01-2000 0.45

我有这样的数据

co_code company_name co_stkdate dailylogreturn
1        A           01-01-2000  0.76
1        A           02-01-2000  0.75
.
.
.
1        A           31-12-2019  0.54
2        B           01-01-2000  0.98
2        B           02-01-2000  0.45
 co_code company_name co_stkdate weeklyreturns
    1        A           07-01-2000  1.34
    1        A           14-01-2000  0.95
    .
    .
    .
    1        A           31-12-2019  0.54
    2        B           07-01-2000  0.98
    2        B           14-01-2000  0.45
library(dplyr)
### Reading txt file
df <- read.csv("33339_1_120_20190405_165913_dat.csv")
。 . 等等

我想找到每周收益,它等于一周内每日日志收益的总和

输出应该是这样的

co_code company_name co_stkdate dailylogreturn
1        A           01-01-2000  0.76
1        A           02-01-2000  0.75
.
.
.
1        A           31-12-2019  0.54
2        B           01-01-2000  0.98
2        B           02-01-2000  0.45
 co_code company_name co_stkdate weeklyreturns
    1        A           07-01-2000  1.34
    1        A           14-01-2000  0.95
    .
    .
    .
    1        A           31-12-2019  0.54
    2        B           07-01-2000  0.98
    2        B           14-01-2000  0.45
library(dplyr)
### Reading txt file
df <- read.csv("33339_1_120_20190405_165913_dat.csv")
我尝试在quantmod包中应用函数,但这些函数仅适用于xts对象。xts对象中的另一个问题是不能使用函数“group_by()”。因此,我只想在通常的数据帧中工作

代码看起来像这样

co_code company_name co_stkdate dailylogreturn
1        A           01-01-2000  0.76
1        A           02-01-2000  0.75
.
.
.
1        A           31-12-2019  0.54
2        B           01-01-2000  0.98
2        B           02-01-2000  0.45
 co_code company_name co_stkdate weeklyreturns
    1        A           07-01-2000  1.34
    1        A           14-01-2000  0.95
    .
    .
    .
    1        A           31-12-2019  0.54
    2        B           07-01-2000  0.98
    2        B           14-01-2000  0.45
library(dplyr)
### Reading txt file
df <- read.csv("33339_1_120_20190405_165913_dat.csv")
库(dplyr)
###读取txt文件

df由于我没有样本数据,我认为这应该可以:

df %>%
   group_by(group = ceiling((1:nrow(df)/ 7))) %>%
   summarise(mean = mean(weeklyreturns))

由于我们不知道您每周有多少天收到
dailylogreturn
,因此可能存在NAs,我建议按周和年进行分组:

#sample data
df <-   data.frame(co_stkdate = rep(seq.Date(from = as.Date("2000-01-07"), to = as.Date("2000-02-07"), by = 1), 2),
                   dailylogreturn = abs(round(rnorm(64, 1, 1), 2)),
                   company_name = rep(c("A", "B"), each = 32))


df %>%
  mutate(co_stkdate = as.POSIXct(co_stkdate),
         year = strftime(co_stkdate, "%W"),
         week = strftime(co_stkdate, "%Y")) %>%
  group_by(company_name, year, week) %>%
  summarise(weeklyreturns = sum(dailylogreturn, na.rm = TRUE))

# A tibble: 12 x 4
# Groups:   company_name, year [12]
   company_name year  week  weeklyreturns
   <fct>        <chr> <chr>         <dbl>
 1 A            01    2000           6.31
 2 A            02    2000           6.11
 3 A            03    2000           6.02
 4 A            04    2000           8.27
 5 A            05    2000           4.92
 6 A            06    2000           0.5 
 7 B            01    2000           1.82
 8 B            02    2000           6.6 
 9 B            03    2000           7.55
10 B            04    2000           7.63
11 B            05    2000           7.54
12 B            06    2000           1.03


#示例数据
df%
突变(co_stkdate=as.POSIXct(co_stkdate),
年份=strftime(co_stkdate,“%W”),
周=标准时间(共同标准日期,“%Y”)%>%
分组依据(公司名称、年份、周)%>%
总结(weeklyreturns=sum(dailylogreturn,na.rm=TRUE))
#一个tibble:12x4
#集团:公司名称,年份[12]
公司名称年度周周返回
1 A 01 2000 6.31
2 A 02 2000 6.11
3 A 03 2000 6.02
4 A 04 2000 8.27
5 A 05 2000 4.92
6 A 06 2000 0.5
7 B 01 2000 1.82
8 B 02 2000 6.6
9 B 03 2000 7.55
10 B 04 2000 7.63
11 B 05 2000 7.54
12 B 06 2000 1.03

根据我的交易知识,我了解到几乎所有交易所都有节假日和收盘日(周六和周日)。您需要创建一个数据集,该数据集应包含这些天,但这些天的交易回报率为0,以便在一年中整整52(或53)周(取决于闰年和非闰年)。这是一个典型的练习。但是,如果您想继续使用实际数据,请使用天花板()按7天对数据进行分组。类似于
DF%>%groupby(group=ceiling((1:nrow(DF)/7))
然后计算每个组的平均值。如何进行“典型练习”?有没有关于增加Daysure的想法。一种方法是填补这些天,因为这些是假期,我们通常采取最后一个交易日的收盘价在这些天的开盘价和收盘价也。这样做是为了避免任何日期,超过实际52或53周。谢谢。这对我有用。但是,我得到的产出大约是00_2000 00_2001 00_2002 00_2003…然后是01_2000 01_2001。我该如何安排该列?嗯,如果您喜欢订单,您可以将
yearweek
列分为
week
year
两列,按两列(+company_name)分组,然后按
week
year
进行安排(如有必要)。非常感谢。