问题
Data in Excel file looks like
TIMESTAMP TYPE BID BIDSIZ
2015-01-04 09:00:00 BID 365 10
2015-04-01 09:00:05 BID 367.8 55
2015-04-01 09:00:33 BID 365 10
2015-04-01 09:00:36 BID 367.8 55
When I run the following code:
require(xlsx)
f1<-read.xlsx2("Canara_Data.xlsx", sheetName = "BID")
f1$TIMESTAMP<-as.POSIXct(f1$TIMESTAMP, format="%Y-%M-%D %H:%M:S")
viewing it causes TIMESTAMP to look like
View(`f1`)
TIMESTAMP X. BID BIDSIZ
42008.375 BID 365 10
42095.37505787037 BID 367.8 55
42095.37538194445 BID 365 10
42095.37541666667 BID 367.8 55
str(f1)
# 'data.frame': 18214 obs. of 4 variables:
# $ TIMESTAMP: POSIXct, format: NA NA ...
# $ TYPE : Factor w/ 1 level "BID": 1 1 1 1 1 1 1 1 1 1 ...
# $ BID : Factor w/ 344 levels "365","365.1",..: 1 55 1 55 1 55 1 55 59 1 ...
# $ BIDSIZ : Factor w/ 1259 levels "1","10","100",..: 2 854 2 854 2 854 2 854 4 2
Please help in making TIMESTAMP to be read as date with the format "%Y-%M-%D %H:%M:S", and also BID and BIDSIZ as character.
回答1:
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))
回答2:
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
来源:https://stackoverflow.com/questions/31917129/error-reading-timestamps-using-xlsx-package