1

我正在尝试将旧金山的所有日期房屋按年出售。我正在使用以下代码

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

有谁知道发生了什么?

4

1 回答 1

0

给出的错误应该向您表明问题不是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)
于 2013-04-11T00:45:28.460 回答