将日期格式转换为数字时出错

时间:2017-12-08 17:18:16

标签: r

我在PMwR包中运行某个函数时收到日期错误。似乎我的日期不是数字,但是,将它们转换为数字会产生NAs。

祝福,

> pl(DaxH1)
Error in Math.data.frame(amount) :
  non-numeric variable in data frame: timestampprice


> dput(head(DaxH1))
structure(list(timestamp = c("2008-01-14 11:00:59", "2008-01-14 13:00:59",
"2008-01-16 17:00:59", "2008-01-17 11:00:59", "2008-01-22 16:00:59",
"2008-01-23 09:07:59"), amount = c(19L, -19L, 20L, -20L, 22L,
-22L), price = c("7735.37", "7741.90", "7485.11", "7528.99",
"6651.13", "6769.17")), .Names = c("timestamp", "amount", "price"
), row.names = c(NA, 6L), class = "data.frame")

> summary(DaxH1)
  timestamp             amount       price         
 Length:624         Min.   :-51   Length:624       
 Class :character   1st Qu.:-32   Class :character 
 Mode  :character   Median :  0   Mode  :character 
                    Mean   :  0                    
                    3rd Qu.: 32                    
                    Max.   : 51    

1 个答案:

答案 0 :(得分:0)

您的数据采用字符格式。将其转换为时间格式,然后您可以转换为数字。

> as.numeric(as.POSIXct(xx$timestamp ))
[1] 1200337259 1200344459 1200531659 1200596459 1201046459 1201108079