“cut”函数中存在错误 我想把旧金山所有的房子都按年出售。我正在使用以下代码 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")))

“cut”函数中存在错误 我想把旧金山所有的房子都按年出售。我正在使用以下代码 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"))),r,cut,R,Cut,有人知道发生了什么吗?给出的错误应该向您表明问题不是截止日期而是截止日期。它向你抱怨无法确定日期的格式 更具体地说,它是您将givn作为标签的内容。不需要把它们包装成日期 标签应为字符和c。引号就足够了 只需稍加修改,上面的代码就可以在几个方面进行清理。 此外,lubridate包可能对您非常有用 # instead of: geo_big$month <- as.Date(paste0(strftime(geo_big$date, format = "%Y-%m"), "-01"))

有人知道发生了什么吗?

给出的错误应该向您表明问题不是截止日期而是截止日期。它向你抱怨无法确定日期的格式

更具体地说,它是您将givn作为标签的内容。不需要把它们包装成日期

标签应为字符和c。引号就足够了

只需稍加修改,上面的代码就可以在几个方面进行清理。 此外,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)

geo_big$date存储为什么格式?as.Datestrptimegeo_big$date,%Y-%m-%dOne看起来可疑的方面是'labels'参数。应该是字符向量而不是日期。在查看helpcut.Date之后,另一个看起来有问题的方面是breaks参数。使用日期值序列进行测试会为我返回一个错误。
# 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)