R日期格式-将字符串转换为日期 [英] R date format - cast string as date
本文介绍了R日期格式-将字符串转换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从数据帧中读取一个日期列,该日期列存储为字符串|(请参见 DateString列。这就是我的数据的样子:
I am trying to read a date column from a dataframe, which is stored as a string |(see column 'DateString'. This is how my data looks like:
X. Date_String ASIN Stars positive_rating
1 0 20150430 B00GKKI4IE 5 0
2 1 20150430 B00GKKI4IE 5 0
3 2 20150430 B00GKKI4IE 5 0
4 3 20150429 B00GKKI4IE 5 0
5 4 20150428 B00GKKI4IE 5 0
6 5 20150428 B00GKKI4IE 5 0
,这就是我用来将此列设置为日期的格式
and this is what I am using to format this column as Date
data$date.of.review <- as.Date(data$Date_String, "%Y%m%d")
收到错误消息
Error in charToDate(x) :
character string is not in a standard unambiguous format
有什么办法解决吗?谢谢
any ideas how to solve it? thanks
他re是数据框的结构:
Here is the structure of the dataframe:
structure(list(X. = 0:5, Date_String = c(20150430L, 20150430L,
20150430L, 20150429L, 20150428L, 20150428L), ASIN = structure(c(1L,
1L, 1L, 1L, 1L, 1L), .Label = c("B00GKKI4IE", "B00I4OBXWI", "B00IB17BFM",
"B00IN2WD5C", "B00J58F0IA", "B00K7NCS9G", "B00KJEZIBS", "B00KZER5GS",
"B00MK39H68", "B00O1GTTWY"), class = "factor"), Stars = c(5L,
5L, 5L, 5L, 5L, 5L), positive_rating = c(0, 0, 0, 0, 0, 0)), .Names = c("X.",
"Date_String", "ASIN", "Stars", "positive_rating"), row.names = c(NA,
6L), class = "data.frame")
推荐答案
如果包装了
sapply(data, class)
data$date.of.review <- as.Date(as.character(data$Date_String), "%Y%m%d")
data
这篇关于R日期格式-将字符串转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文