2017-09-02 38 views
1

我有一个数据框在R,我已经从CSV导入。在CSV的 “时间” 的格式是 “%Y-%间 - %d%H:%M:%S”,例如:lubridate转换午夜时间戳返回不适用:如何填写缺少的时间戳

> head(btc_data) 
       time btc_price 
1 2017-08-27 22:50:00 4,389.6113 
2 2017-08-27 22:51:00 4,389.0850 
3 2017-08-27 22:52:00 4,388.8625 
4 2017-08-27 22:53:00 4,389.7888 
5 2017-08-27 22:56:00 4,389.9138 
6 2017-08-27 22:57:00 4,390.1663 

当运行str(btc_data)时间列回来为一个因素。

btc_data$time <- ymd_hms(as.character(btc_data$time)) 

的问题是在午夜(5行)中收集的数据无法解析,并返回NA值像这样(在原始数据的时间戳:因此,我已经采用lubridate包如下转化这对日期时间从这些行,以便2017-08-29 00:00:00列出简称为2017-08-29)缺失 -

724 2017-08-28 23:59:00 4,439.3313 
725 NA     4,439.6588 
726 2017-08-29 00:01:00 4,440.3050 

此外,第二数据帧被不同地组织:

> str(eth_data) 
'data.frame': 1081 obs. of 2 variables: 
$ time  : Factor w/ 1081 levels "8/28/17 16:19",..: 1 2 3 4 5 6 7 8 9 10 ... 
$ eth_price: num 344 344 344 344 343 ... 

当我尝试:

> eth_data$time <- mdy_hms(as.character(eth_data$time)) 

我收到以下错误:

Warning message: All formats failed to parse. No formats found.

编辑:我已经分离的代码问题是造成问题的原因:

> btc_data[721:726,] 
        time btc_price 
721 2017-08-28 23:57:00 4,439.8163 
722 2017-08-28 23:58:00 4,440.2363 
723 2017-08-28 23:58:00 4,440.2363 
724 2017-08-28 23:59:00 4,439.3313 
725 2017-08-29   4,439.6588 
726 2017-08-29 00:01:00 4,440.3050 

所以,每次午夜时钟敲响时,都不记录时间戳。 CSV正在通过数据流创建并不断增长,所以除非我能找到解决方法,否则每个新的一天都会继续发生此问题。有什么建议么?

+0

请包括失败的行。 –

+0

请参阅我的编辑 - 我添加了失败的行,第二个df完全失败。 – zsad512

+0

在lubridate有机会混搭它之前向我们显示* text *。是00:00:00还是24:00:00? – Spacedman

回答

2

如果'00:00:00'在原始数据中完全缺失,可以使用grep找到这些情况,然后在使用ymd_hms()或mdy_hm之前粘贴'00:00:00' ()函数。

第一种情况,其中,日期/时间格式为 'YYYY-MM-DD HH:MM:SS':

#Before 
test <- fread("time, btc_price 
2017-08-28 23:57:00, 4439.8163 
2017-08-28 23:58:00, 4440.2363 
2017-08-28 23:58:00, 4440.2363 
2017-08-28 23:59:00, 4439.3313 
2017-08-29   , 4439.6588 
2017-08-29 00:01:00, 4440.3050") 

test$time[grep("[0-9]{4}-[0-9]{2}-[0-9]{2}$",test$time)] <- paste(
    test$time[grep("[0-9]{4}-[0-9]{2}-[0-9]{2}$",test$time)],"00:00:00") 

#After 
print(test) 

        time btc_price 
1: 2017-08-28 23:57:00 4439.816 
2: 2017-08-28 23:58:00 4440.236 
3: 2017-08-28 23:58:00 4440.236 
4: 2017-08-28 23:59:00 4439.331 
5: 2017-08-29 00:00:00 4439.659 
6: 2017-08-29 00:01:00 4440.305 

#Now you can use ymd_hms(as.character(df$date)) as usual. 

第二种情况,其中,日期/时间格式是“米/ DD/YY HH: MM':

#Step 1 is to find/replace: 
test <- fread("time, btc_price 
8/28/17 23:57, 4439.8163 
8/28/17 23:57, 4440.2363 
8/28/17 23:57, 4440.2363 
8/28/17 23:57, 4439.3313 
8/28/17  , 4439.6588 
8/29/17 00:01, 4440.3050") 

test$time[grep("[0-9]{1}/[0-9]{2}/[0-9]{2}$",test$time)] <- paste(
    test$time[grep("[0-9]{1}/[0-9]{2}/[0-9]{2}$",test$time)],"00:00" 
) 

print(test) 
      time btc_price 
1: 8/28/17 23:57 4439.816 
2: 8/28/17 23:57 4440.236 
3: 8/28/17 23:57 4440.236 
4: 8/28/17 23:57 4439.331 
5: 8/28/17 00:00 4439.659 
6: 8/29/17 00:01 4440.305 

#Step 2 is to adjust your mdy_hms() command; you need to leave off the 's': 
#Ex. before: 
mdy_hms(as.character("8/28/17 16:19")) 
[1] NA 
Warning message: 
All formats failed to parse. No formats found. 

#After 
test <- c("8/28/17 16:19","8/28/17 00:00") 
mdy_hm(as.character(test)) 
[1] "2017-08-28 16:19:00 UTC" "2017-08-28 00:00:00 UTC" 

一般来说,数字在R中没有逗号格式化也是一个好习惯。所以4,439.3313应该是4439.3313。否则,R可能会将其解释为列之间的逗号分隔。

+0

请参阅已编辑的问题,问题不在'btc_price'列(正在读取正确)中,而是在'date'列中丢失错误的午夜时间戳。 – zsad512

+0

我觉得你还是不理解。对于'2017-08-29',你已经添加了'00:00:00 UTC'的时间戳,但是如果仔细查看我的问题,这是从数据中丢失的 - 这是潜在的问题....如何插入00 :00:00在日期时间内缺少时间戳的地方。 – zsad512

+0

@ zsad512 - 现在你已经澄清了这个问题,我已经编辑了我的答案。查看上面的更新。 – www