首页 文章

R中的日期值到日期格式

提问于
浏览
0

我正在尝试更改从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"

我试图找到一些关于这个问题的帖子,但我找不到任何内容,请帮助我解决方案或链接解决方案谢谢

2 回答

  • 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>
    
  • 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"
    

相关问题