Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
lubridate转换午夜时间戳返回NA:如何填充丢失的时间戳_R_Timestamp_Lubridate - Fatal编程技术网

lubridate转换午夜时间戳返回NA:如何填充丢失的时间戳

lubridate转换午夜时间戳返回NA:如何填充丢失的时间戳,r,timestamp,lubridate,R,Timestamp,Lubridate,我有一个从CSV导入的R格式的数据帧。csv中的“时间”格式为“%Y-%m-%d%H:%m:%S”,如下所示: 当我运行str(btc_数据)时,时间列作为一个因素返回。因此,我使用lubridate包将其转换为datetime,如下所示: btc_data$time <- ymd_hms(as.character(btc_data$time)) 此外,第二数据帧被不同地组织: > str(eth_data) 'data.frame': 1081 obs. of 2 var

我有一个从CSV导入的R格式的数据帧。csv中的“时间”格式为“%Y-%m-%d%H:%m:%S”,如下所示:

当我运行
str(btc_数据)
时,时间列作为一个因素返回。因此,我使用lubridate包将其转换为datetime,如下所示:

btc_data$time <- ymd_hms(as.character(btc_data$time)) 
此外,第二数据帧被不同地组织:

> 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))

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

如果开始时原始数据中完全缺少“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.
#之前

测试请包括失败的行。请查看我的编辑-我添加了失败的行,第二个df完全失败。在lubridate有机会将其捣碎之前,向我们显示文本。是00:00:00还是24:00:00?手动添加这些元素的时间:
使用(df,ifelse(nchar(date)==10,粘贴(date,“00:00:00”),日期)
,然后使用
转换为.POSIXct
只需执行
btc_数据$time请查看编辑的问题,问题不在于
btc_价格
列(该列正在阅读中)相反,在抛出错误的
date
列中缺少午夜的时间戳。我想你还是不明白。对于
2017-08-29
您已经添加了时间戳
00:00:00 UTC
,但是如果您仔细看我的问题,数据中缺少了时间戳,这就是根本问题……如何在datetime中缺少时间戳的地方插入00:00:00。@zsad512-现在您已经澄清了问题,我已经编辑了我的答案。请参阅上面的更新。第二个数据框中的日期列为
m/d/y
?我仍然在那里遇到了一个错误:
所有格式都无法解析
我现在添加了完整的示例,从开始到结束都介绍了如何解析。
> eth_data$time <- mdy_hms(as.character(eth_data$time))
> 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
#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.
#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"