我正在尝试更改从CSV文件导入日期的格式。日期列的类是因子,但是当我想使用“as.Date”函数时,它会让我误认为字符串格式不合适。
> New_His_data$BirthDate[1]
[1] 36473
1378 Levels: 13-1-1990 13-1-1991 13-10-1959 13-10-1973 13-10-1979 13-10-1988 13-10-1989 13-10-1994 13-11-1963 13-11-1970 ... 36473
> as.Date(New_His_data$BirthDate[1],origin = "1899-12-30")
Error in charToDate(x) :
character string is not in a standard unambiguous format
> as.Date(strptime(New_His_data$BirthDate[1],"%d-%m-%Y"),origin ="1899-12-30")
[1] NA
当我使用数值作为数字时,它正常工作
> as.Date(36473,origin = "1899-12-30")
[1] "1999-11-09"
我试图找一些关于这个问题的帖子,但我找不到任何东西,请帮助我解决方案或解决方案的链接 谢谢
答案 0 :(得分:0)
您的数据作为factor
变量传入,可能通过read.csv()
或未设置StringsAsFactors=FALSE
时相关。
转换为字符无法修复。模拟示例:
R> v <- factor(c("13-1-1990", "13-1-1991", "13-10-1959")) # your first three
R> v
[1] 13-1-1990 13-1-1991 13-10-1959
Levels: 13-1-1990 13-1-1991 13-10-1959
R>
R> d <- as.Date(as.character(v), format="%d-%m-%Y")
R> d ## key here were the as.character() and the correct format
[1] "1990-01-13" "1991-01-13" "1959-10-13"
R>
R> class(d) ## these are now Date objects
[1] "Date"
R>
R> d + 1 ## that we can compute with
[1] "1990-01-14" "1991-01-14" "1959-10-14"
R>
答案 1 :(得分:0)
我可以解决这个问题,只需将数据类型从日期转换为字符串再转换为数字,然后我使用“as.Date”函数来更改格式。
> New_His_data$BirthDate[1]
[1] 36473
1378 Levels: 13-1-1990 13-1-1991 13-10-1959 13-10-1973 13-10-1979 13-10-1988 13-10-1989 13-10-1994 13-11-1963 13-11-1970 ... 36473
> as.Date(New_His_data$BirthDate[1],origin = "1899-12-30")
Error in charToDate(x) :
character string is not in a standard unambiguous format
> as.Date(strptime(New_His_data$BirthDate[1],"%d-%m-%Y"),origin ="1899-12-30")
[1] NA
> as.Date(as.numeric(as.character(New_His_data$BirthDate[1])),origin = "1899-12-30")
[1] "1999-11-09"