请考虑这个小数据.框架.我正在试着写一篇基于 SampleDate已格式化为.Date,但新创建的Year列 变成了一个角色,而不是日期.问题是,我如何保留年份列的格式? try 转换为as.Date时出错. 我的代码
a <- structure(list(SampleRowID = c(109, 109, 109, 109, 109, 109),
SampleDate = structure(c(15803, 15803, 15803, 15803, 15803,
15803), class = "Date")), class = "data.frame", row.names = c(NA,
-6L))
glimpse(a)
$ SampleRowID <dbl> 109, 109, 109, 109, 109, 109
$ SampleDate <date> 2013-04-08, 2013-04-08, 2013-04-08, 2013-04-08, 2013-04-08, 2013-04-08
#SampleDate is a date format
#Adding a year column doesn't keep the date format...is a character
a$year <- format(a$SampleDate,"%Y")
glimpse(a)
$ SampleRowID <dbl> 109, 109, 109, 109, 109, 109
$ SampleDate <date> 2013-04-08, 2013-04-08, 2013-04-08, 2013-04-08, 2013-04-08, 2013-04-08
$ year <chr> "2013", "2013", "2013", "2013", "2013", "2013"
try 将年份转换为as.Date格式时引发错误
A$Year<;-as.Date(A$Year) CharToDate(X)中出错: 字符串不是标准的明确格式