【问题标题】:Smartbind date format and error in RR中的Smartbind日期格式和错误
【发布时间】:2014-08-10 10:32:01
【问题描述】:

我在使用 smartbind 附加两个数据集时遇到错误。首先,我很确定我得到的错误:

> Error in as.vector(x, mode) : invalid 'mode' argument

来自两个数据集中的日期变量。其原始格式的日期变量是这样的:月/日/年。我使用 as.Date 和 format 导入数据后转换了变量

> rs.month$xdeeddt <- as.Date(rs.month$xdeeddt, "%m/%d/%Y")
> rs.month$deed.year <-  as.numeric(format(rs.month$xdeeddt, format = "%Y"))
> rs.month$deed.day <-  as.numeric(format(rs.month$xdeeddt, format = "%d"))
> rs.month$deed.month <-  as.numeric(format(rs.month$xdeeddt, format = "%m"))

生成的日期变量如下:

> [1] "2014-03-01" "2014-03-13" "2014-01-09" "2013-10-09"

日期转换应用于两个数据集(两个数据集的原始数据格式相同)。当我尝试使用 gtools 包中的 smartbind 来附加两个数据集时,它返回并出现上述错误。我从两个数据集中删除了日期、月份、日期和年份变量,并且能够使用 smartbind 成功附加数据集。

关于如何将数据集附加到日期变量的任何建议.....?

【问题讨论】:

  • 您运行的是哪个版本的gtools?我看到在gtools_3.1.1 中看到了该错误,但在gtools_3.4.1 中没有看到
  • 我有3.0.0版本,我会更新试试看。

标签: r date append


【解决方案1】:

在两个数据帧的智能绑定期间,我在谷歌上搜索了相同的错误消息后来到这里。上面的讨论虽然对解决方案没有那么确定,但绝对帮助我克服了这个错误。

我的两个数据框都包含 POSIXct 日期对象。这些只是 UNIXy seconds-since-epoch 的数字向量,以及提供将向量解释为日期对象所需的结构的几个属性。解决方案是简单地从该变量中剥离属性,执行智能绑定,然后恢复属性:

these.atts <- attributes(df1$date)
attributes(df1$date) <- NULL
attributes(df2$date) <- NULL
df1 <- smartbind(df1,df2)
attributes(df1$date) <- these.atts

我希望这对某人有所帮助。

-安迪

【讨论】:

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