【问题标题】:R converts as.Date to different data type when assigning to vector or dataframeR在分配给向量或数据帧时将 as.Date 转换为不同的数据类型
【发布时间】:2021-05-10 22:58:03
【问题描述】:

我正在尝试将 Date 对象分别存储在向量和 data.frame 中。
不过,似乎有些麻烦。当我将对象分配给向量时,它似乎被转换为数字数据类型。以下代码

print(as.Date("11-Jan-2011",format="%d-%b-%Y"))
test_v <-c()
test_v[1]<-as.Date("11-Jan-2011",format="%d-%b-%Y")
print(test_v)
print(str(test_v))

给出输出

[1] "2011-01-11"
[1] 14985
num 14985
NULL

类似地,当我尝试将 Date 对象分配给 data.frame 的特定行和列时,它再次被转换,但这次转换为字符。

test_df <- data.frame(a=c(1:3))
test_df$b <- 999
test_df[1,]$b <- "any string"
test_df[2,]$b <- 2
test_df[3,]$b <- as.Date("11-Jan-2011",format="%d-%b-%Y")
print(test_df$b)
str(test_df$b)
str(test_df[3,]$b)

结果

[1] "any string" "2"          "14985"  
chr [1:3] "any string" "2" "14985"
chr "14985"

但是,将日期对象分配给 data.frame 的整个列似乎可行。

test_df$c <- as.Date("11-Jan-2011",format="%d-%b-%Y")
str(test_df)

结果

'data.frame':   3 obs. of  3 variables:
$ a: int  1 2 3
$ b: chr  "any string" "2" "14985"
$ c: Date, format: "2011-01-11" "2011-01-11" "2011-01-11"

发生了什么事?

【问题讨论】:

    标签: r dataframe


    【解决方案1】:

    日期对象定义为自开始(1970 年 1 月 1 日)以来的整数天数。
    向量和数据框列只能存储一种类型的对象

    因此,当您尝试将 Date 对象存储在现有数据结构中时,Date 对象将被转换为最常见的对象类型。在上述情况下,日期对象先转换为整数,然后再转换为字符串。

    要保留日期格式,请在赋值前使用format 函数。

    test_df <- data.frame(a=c(1:3))
    test_df$b <- 999
    test_df[1,]$b <- "any string"
    test_df[2,]$b <- 2
    test_df[3,]$b <- format(as.Date("11-Jan-2011",format="%d-%b-%Y"), "%m/%d/%y")
    print(test_df$b)
    #[1] "any string" "2"          "01/11/11"  
    

    【讨论】:

      【解决方案2】:

      使用data.framedata.frame 类似的对象(例如data.tabletbl)。他们保留班级:

      > df <- data.frame(A=as.Date("2011-01-11") + 0:3, B=Sys.Date() + 0.3)
      > sapply(df, class)
           A      B 
      "Date" "Date" 
      >
      

      将它们保留为Date 对象具有您仍然可以计算它们的优势。您仍然可以通过format() 转换为character

      > df$diff <- with(df, difftime(B, A))
      > df
                 A          B        diff
      1 2011-01-11 2021-02-06 3679.3 days
      2 2011-01-12 2021-02-06 3678.3 days
      3 2011-01-13 2021-02-06 3677.3 days
      4 2011-01-14 2021-02-06 3676.3 days
      > 
      > df$diffInt <- with(df, as.integer(difftime(B, A)))
      > df
                 A          B        diff diffInt
      1 2011-01-11 2021-02-06 3679.3 days    3679
      2 2011-01-12 2021-02-06 3678.3 days    3678
      3 2011-01-13 2021-02-06 3677.3 days    3677
      4 2011-01-14 2021-02-06 3676.3 days    3676
      > 
      

      两种格式为例:

      > df$yyymmdd <- format(df$A, "%Y%m%d")
      > df$montxt <- format(df$B, "%Y-%B-%d")
      > df
                 A          B        diff diffInt  yyymmdd           montxt
      1 2011-01-11 2021-02-06 3679.3 days    3679 20110111 2021-February-06
      2 2011-01-12 2021-02-06 3678.3 days    3678 20110112 2021-February-06
      3 2011-01-13 2021-02-06 3677.3 days    3677 20110113 2021-February-06
      4 2011-01-14 2021-02-06 3676.3 days    3676 20110114 2021-February-06
      > 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-20
        • 2020-12-06
        • 2017-12-30
        • 1970-01-01
        • 2023-01-18
        • 2021-07-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多