【发布时间】:2015-09-08 09:12:09
【问题描述】:
我的问题是as.Date 函数不会将数据框的date 列中的值转换为Date objects。
我有一个data.frame nmmaps。这是其中的一部分。
city date death temp dewpoint pm10 o3 time season
1 chic 1/1/1987 130 31.5 31.500 27.79119 4.025928 1 winter
2 chic 1/2/1987 150 33.0 29.875 NA 4.579652 2 winter
3 chic 1/3/1987 101 33.0 27.375 33.67382 3.400928 3 winter
4 chic 1/4/1987 135 29.0 28.625 40.79119 3.942595 4 winter
5 chic 1/5/1987 126 32.0 28.875 NA 4.400928 5 winter
6 chic 1/6/1987 130 40.0 35.125 41.79119 5.984261 6 winter
我使用以下命令从 Excel 文件导入数据:
nmmaps <- read.csv("chicago-nmmaps.csv" , as.is = T)
当我到达 as.Date 转换点时,我输入
nmmaps$date <- as.Date(nmmaps$date) 并获得如下所示的data.frame。
city date death temp dewpoint pm10 o3 time season
1 chic 0001-01-19 130 31.5 31.500 27.79119 4.025928 1 winter
2 chic 0001-02-19 150 33.0 29.875 NA 4.579652 2 winter
3 chic 0001-03-19 101 33.0 27.375 33.67382 3.400928 3 winter
4 chic 0001-04-19 135 29.0 28.625 40.79119 3.942595 4 winter
5 chic 0001-05-19 126 32.0 28.875 NA 4.400928 5 winter
6 chic 0001-06-19 130 40.0 35.125 41.79119 5.984261 6 winter
为什么日期会这样显示?此外,某些日期有 NA 字段。我希望将年份显示为 1987、1988、1989 等。
【问题讨论】:
-
拒绝(显然)未能阅读
as.Date的文档或对 SO 进行任何搜索。 -
您应该提供
as.Date函数的format参数,以便“告诉”如何解析日期。 -
@BondedDust 我确实通过查看文档。 ?as.Date()。我只是错过了有默认格式的事实。