【问题标题】:section a datetime column by just dates, not time仅按日期而不是时间来划分日期时间列
【发布时间】:2016-12-27 18:59:24
【问题描述】:

我有两个数据框。数据框 1 由 DATETIME 列索引。

格式为YYYY-MM-DD HH:MM:SS

Dataframe 2 只有一列,选择日期的列表,格式为:YYYY-MM-DD

我想按数据框 2 中的日期划分数据框 1。

我不确定如何让 R 忽略 Dataframe 1 的 DATETIME 列的 HH:MM:SS 部分

我试过match(meetingDates$Date, days1993$DATETIME) 但返回了一堆 NA 的

我还尝试向 Dataframe 1 添加一个新列,它只是 DATETIME 列 days1993$DATE <- ymd(days1993$DATETIME) 的日期部分,但它没有用。

Warning message:
All formats failed to parse. No formats found. 

有什么建议吗?

【问题讨论】:

  • as.Date(days1993$DATETIME) 将只将该列转换为日期,删除一天中的时间。

标签: r date datetime


【解决方案1】:

如果没有可重现的示例,则无法确定问题所在(有多种可能性),但请尝试将 as.Date() 放在 Dataframe 1 中的列周围。请参阅下面的演示,了解其工作原理:

> x1 <- strptime("2012-06-15 12:15:42", format = "%Y-%m-%d %H:%M:%S")
> x2 <- as.Date("2012-06-15")
> x1
[1] "2012-06-15 12:15:42 NZST"
> x2
[1] "2012-06-15"
> x2 == x1
   sec    min   hour   mday    mon   year   wday   yday  isdst   zone gmtoff 
 FALSE  FALSE  FALSE  FALSE  FALSE  FALSE  FALSE  FALSE  FALSE     NA     NA 
Warning messages:
1: Incompatible methods ("Ops.Date", "Ops.POSIXt") for "==" 
2: NAs introduced by coercion 
> x2 == as.Date(x1)
[1] TRUE

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-19
    • 2015-04-25
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 2016-10-28
    相关资源
    最近更新 更多