1

有没有办法让 R 中的日期为 2016 年 3 月 31 日(不显示该月的 03)?

现在我的代码是:

dates <- seq(as.Date("2000-1-1"), as.Date("2016-10-1"), by="3 months") -1
dates <- format(dates, format = "%m/%d/%Y")

但结果是

[1] "12/31/1999" "03/31/2000" "06/30/2000" "09/30/2000"

谢谢

4

1 回答 1

0

我们可以使用sub, 匹配字符串开头 ( ^) 处的 '0' 模式并将其替换为空格 ( "")。

dates1 <- sub("^0", "", dates)
head(dates1)
#[1] "12/31/1999" "3/31/2000"  "6/30/2000"  "9/30/2000"  "12/31/2000" "3/31/2001" 

注意:尚不清楚为什么不需要标准格式。

于 2016-10-04T13:23:32.073 回答