Error reading timestamps using “xlsx” package

你说的曾经没有我的故事 提交于 2019-12-06 07:24:54

Excel & date formats are often not a good combination. You can use:

f1$TIMESTAMP <- as.POSIXct(f1$TIMESTAMP*86400, origin="1899-12-30",tz="GMT")

to convert it to a datatime format.

This gives:

> f1
            TIMESTAMP  X.   BID BIDSIZ
1 2015-01-04 09:00:00 BID 365.0     10
2 2015-04-01 09:00:05 BID 367.8     55
3 2015-04-01 09:00:33 BID 365.0     10
4 2015-04-01 09:00:36 BID 367.8     55

Another solution is to export your excel-file to a .csv or a tab-separated .txt file and then read it into R.

You can convert the BID and BIDSIZ columns to character columns with:

f1[,c(3:4)] <- lapply(f1[,c(3:4)], as.character)

Used data:

f1 <- structure(list(TIMESTAMP = c(42008.375, 42095.3750578704, 42095.3753819444, 42095.3754166667),
                     X. = structure(c(1L, 1L, 1L, 1L), .Label = "BID", class = "factor"), 
                     BID = c(365, 367.8, 365, 367.8),
                     BIDSIZ = c(10L, 55L, 10L, 55L)),
                .Names = c("TIMESTAMP", "X.", "BID", "BIDSIZ"), class = "data.frame", row.names = c(NA, -4L))
gaurav kumar

Use Package readxl

excel_sheets("C:\\Users\\Gaurav Kumar\\Documents\\Canara_Data.xlsx")
grv<-read_excel("C:\\Users\\Gaurav Kumar\\Documents\\Canara_Data.xlsx", sheet = 1, col_names = TRUE, col_types = NULL, na = "", skip = 0)

and it will read the Timestamps too. It saves huge time in converting the excel to csv, knowing CSV has a problem in saving timestamps.

              TIMESTAMP EX  BID BIDSIZ
1   2015-04-01 09:00:00 N   365.00  10
2   2015-04-01 09:00:05 N   367.80  55
3   2015-04-01 09:00:33 N   365.00  10
4   2015-04-01 09:00:36 N   367.80  55
5   2015-04-01 09:00:41 N   365.00  10
6   2015-04-01 09:00:41 N   367.80  55
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!