【问题标题】:Merging Embedded Lists with Different Date Formats in R在 R 中合并具有不同日期格式的嵌入列表
【发布时间】:2020-12-01 13:13:01
【问题描述】:

我需要将两个列表相互合并,但我没有得到我想要的,我认为这是因为"Date" 列有两种不同的格式。我有一个名为 li 的列表,在这个列表中有 12 个列表,每个列表的格式如下:

> tail(li$fxe)
           Date           fxe
3351 2020-06-22  0.0058722768
3352 2020-06-23  0.0044256216
3353 2020-06-24 -0.0044998220
3354 2020-06-25 -0.0027309539
3355 2020-06-26  0.0002832672
3356 2020-06-29  0.0007552346

我正在尝试将这些唯一列表中的每一个与名为 factors 的不同列表合并,如下所示:

> tail(factors)
         Date  Mkt-RF   SMB   HML    RF
3351 20200622  0.0071  0.83 -1.42 0.000
3352 20200623  0.0042  0.15 -0.56 0.000
3353 20200624 -0.0261 -0.52 -1.28 0.000
3354 20200625  0.0112  0.25  0.50 0.000
3355 20200626 -0.0243  0.16 -1.37 0.000
3356 20200629  0.0151  1.25  1.80 0.000

我需要这种结构的原因是因为我试图将它们发送到我编写的用于进行线性回归的函数。但我函数的第一行旨在合并这些列表。当我合并它们时,我最终得到一个空结构,甚至认为我的列表显然具有相同的行数。在我的函数df 中是lili 的嵌入列表让我感到困惑。有人可以帮忙吗?

我要使用的功能:

Bf <- function(df, fac){
#This function calculates the beta of the french fama factor #using linear regression
#Input: df = a dataframe containg returns of the security
# fac = dataframe containing excess market retrun and
# french fama 3 factor
#Output: a Beta vectors of the french fama model
temp <- merge(df, fac, by="Date")
temp <- temp[, !names(temp) %in% "Date"]
temp[ ,1] <- temp[,1] - temp$RF return(lm(temp[,1]~temp[,2]+temp[,3]+temp[,4])$coeff)
}

【问题讨论】:

    标签: r list date merge regression


    【解决方案1】:

    a:您处理的是数据框而不是列表 b:如果你想合并它们,你需要修改factors$date列以匹配li$fxe$date

    尝试做:

    factors$date <- as.Date(strptime(factors$date, format = "%Y%M%d"))
    

    这应该将因素列转换为“日期”格式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-22
      • 1970-01-01
      • 1970-01-01
      • 2021-04-30
      相关资源
      最近更新 更多