【问题标题】:Error in `cut` function`cut` 函数中的错误
【发布时间】:2013-04-11 00:13:31
【问题描述】:

我正在尝试按年份对旧金山的所有日期房屋进行分组。我正在使用以下代码

geo_big$month <- as.Date(paste0(strftime(geo_big$date, format = "%Y-%m"), "-01"))

geo_big$date_r <- cut(geo_big$month, breaks = as.Date(c("2003-04-01", "2004-01-01", "2005-01-01", "2006-01-01", "2007-01-01", "2008-11-01")), include.lowest = TRUE, labels = as.Date(c("2003-01 - 2004-12", "2004-01 - 2004-12", "2005-01 - 2005-12", "2006-01 - 2006-12", "2007-01 - 2007-12", "2008-01 - 2008-11")))

并收到此消息:

Error in charToDate(x) : 
  character string is not in a standard unambiguous format

有人知道怎么回事吗?

【问题讨论】:

  • geo_big$date 存储为什么格式?
  • as.Date(strptime(geo_big$date, "%Y-%m-%d"))
  • 看起来可疑的一个方面是“标签”参数。应该是字符向量而不是日期。看起来有问题的另一个方面(在查看 help(cut.Date) 之后)是中断参数。使用一系列 Date 值进行测试对我来说会返回一个错误。

标签: r cut


【解决方案1】:

给出的错误应该向您表明问题不是cut,而是as.Date。 (它在向你抱怨无法确定日期的格式)

更具体地说,它是您提供的标签。无需将它们包装在as.Date

标签应该是characterc(.),引号就足够了。


就像一点手一样,上面的代码可以在几个方面进行清理。
此外,lubridate 包可能对您非常有用。

# instead of: 
geo_big$month <- as.Date(paste0(strftime(geo_big$date, format = "%Y-%m"), "-01"))

# you can use `floor_date`: 
library(lubridate)
geo_big$month <- floor_date(geo_big$date, "month")  # from the `lubridate` pkg


# instead of: 
... a giant cut statement... 

# use variables for ease of reading and debugging

# bks <- as.Date(c("2003-04-01", "2004-01-01", "2005-01-01", "2006-01-01", "2007-01-01", "2008-11-01")) 
# or: 
bks <- c(dmin, seq.Date(ceiling_date(dmin, "year"), floor_date(dmax, "year"), by="year"), dmax)  # still using library(lubridate)

# basing your labels on your breaks helps guard against human error & typos
lbls <- head(floor_date(bks, "year"), -1)  # dropping the last one, and adding dmax
lbls <- paste( substr(lbls, 1, 7),   substr(c(lbls[-1] - 1, dmax), 1, 7), sep=" - ")

# a cleaner, more readable `cut` statement
cut(geo_big$month, breaks=bks, include.lowest=TRUE, labels=lbls)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-29
    • 2015-05-14
    相关资源
    最近更新 更多