【问题标题】:How to merge two datasets if one is of type factor and the other is of type date in R?如果一个数据集是因子类型,另一个是 R 中的日期类型,如何合并两个数据集?
【发布时间】:2021-02-23 02:56:44
【问题描述】:

我正在使用 edgar 包来查找有关在美国 SEC 系统中上市的公司的具体信息。我需要为所有公司创建一个数据集。对于具有 10-k 报表类型的公司,数据集由包自动创建,使用以下代码:

threeM <- searchFilings(cik.no = "66740", form.type = "10-K", filing.year = c(1996:2018), word.list)
write.csv(threeM, "threeM").

对于没有 10-k 的公司,数据集不是自动创建的,所以我通过以下代码手动构建它们:

 electrolux <- data.frame(cik = "813810",
                         company.name = "AB electrolux",
                         form.type = "NA",
                         date.filed = "NA",
                         nword.hits = "NA"
                         )

我需要创建一个包含所有公司的数据集,并将它们合并在一起。因此,我选择合并以字母 A 开头并具有 10-k 报告的公司的数据集,并将它们与包含所有以字母 A 开头但没有 10-k 的公司的数据集放在一起报告。

所以我使用了命令:

full_join(A1, A2)

但出现了以下答案:

Can't join on `x$date.filed` x `y$date.filed` because of incompatible types. ℹ `x$date.filed` is of type <date>>. ℹ `y$date.filed` is of type <factor<279f2>>>.

我已经尝试将其中一个数据框转换为日期类型,但列被打乱了。

【问题讨论】:

    标签: r date merge


    【解决方案1】:

    dplyr 连接要求数据类型相同。尝试将数据框创建为:

    electrolux <- data.frame(cik = "813810",
                             company.name = "AB electrolux",
                             form.type = NA_character_,
                             date.filed = as.Date(NA),
                             nword.hits = NA_real_, stringsAsFactors = FALSE)
    

    【讨论】:

      猜你喜欢
      • 2012-06-07
      • 2022-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多