【问题标题】:Extracting the day an month into separate columns out of a column with an ambiguous date format [duplicate]将日期格式不明确的列中的某一天提取到单独的列中[重复]
【发布时间】:2020-10-05 20:06:59
【问题描述】:

我有一个如下所示的数据库:

structure(list(State = c("Alaska", "Alaska", "Alaska", "Alaska", 
"Alaska"), month = c(NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_), Abbreviation = c("AK", "AK", "AK", "AK", "AK"), date = c("1/31/2011", 
"10/31/2011", "11/30/2011", "12/31/2010", "4/30/2005"), year = c("2011", 
"2011", "2011", "2010", "2005")), row.names = c(NA, 5L), class = "data.frame")

其中有一列日期为date

我要做的就是将日期和月份提取到单独的列中。

然而,我总是得到日期不明确的消息,即使显然没有第 30 个月或第 31 个月。

但不知何故,似乎没有任何效果。我试过了:

ar203$Month_Yr <- format(as.Date(ar203$date), "%Y-%m")
Error in charToDate(x) : 
  character string is not in a standard unambiguous format

ar203$month <- strftime(ar203$date, "%m")
Error in as.POSIXlt.character(x, tz = tz) : 
  character string is not in a standard unambiguous format

ar203$date <- as.POSIXct(ar203$date)
Error in as.POSIXlt.character(x, tz, ...) : 
  character string is not in a standard unambiguous format

发生了什么,我该如何解决?

【问题讨论】:

    标签: r string date


    【解决方案1】:

    您的日期格式是%m/%d/%Y,因此您必须在as.Date 中说明这一点。

    ar203$Month_Yr <- format(as.Date(ar203$date, "%m/%d/%Y"), "%Y-%m")
    

    【讨论】:

    • 非常感谢,我为此失去了理智..
    猜你喜欢
    • 1970-01-01
    • 2013-05-28
    • 2013-12-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多