2016-12-30 22 views
0

在我試圖從Excel加載數據到R:將數據從Excel加載到R - 第一列中出現額外日期?

> book <- loadWorkbook(file.choose()) 
> signals = readWorksheet(book, sheet = "signals", header = TRUE) 
> signals 

返回:

    time signal1 signal2 
1 1899-12-31 08:30:00 0.43 -0.20 
2 1899-12-31 08:31:00 0.54 0.33 
3 1899-12-31 08:32:00 0.32 -0.21 

爲什麼我與1899年12月31日的專欄?這些絕對不在Excel工作表中。其餘是正確的。

+0

我認爲你可以使用'read_excel'從'庫(readxl)' – akrun

+0

看起來像'readWorksheet'有一個'rownames'說法。你嘗試過使用它嗎?根據[文檔](https://cran.r-project.org/web/packages/XLConnect/XLConnect.pdf),「rownames = NULL」將起作用。 –

+0

最好用*答案*回答你的問題(你可能需要等一會兒,然後才允許),而不是編輯你的問題來合併答案。 –

回答

2

我發現這一段的文檔:

強制從數字轉換爲日期時間:自Excel中瞭解到的日期/時間與一些額外的格式NU-merics,從數字爲DateTime轉換爲AC- tually可能。本例中的數字代表自1900-01-00以來的天數(是,第00天! - 見http://www.cpearson.com/excel/datetime.htm)。請注意,在R 0中表示爲1899-12-31,因爲沒有1900-01-00。分數天代表小時,分鐘和秒。

這似乎揭示了一些問題。

0

下面介紹如何將數據框保存到Excel文件並從Excel文件加載數據框。

library(lubridate) 
library(stringr) 
library(xlsx) 

#Creating time and date data 
t1 <- ymd_hms("2011-06-04 12:00:00") 
t2 <- ymd_hms("2011-06-05 11:00:00") 
t3 <- ymd_hms("2011-06-06 10:00:00") 

#Storing the time and date data in a data frame 
#Storing t1 
df <- data.frame(NULL) 
df <- rbind(df, data.frame(t(strsplit(as.character(t1), " ")), stringsAsFactors = F)) 
colnames(df) <- c("V1") 
df <- str_split_fixed(df$V1, " ", 2) 
df <- gsub("[^0-9.:-]","",df) 
colnames(df) <- c("Date", "Time") 
#Storing t2 
tmp <- data.frame(t(strsplit(as.character(t2), " ")), stringsAsFactors = F) 
colnames(tmp) <- c("V1") 
tmp <- str_split_fixed(tmp$V1, " ", 2) 
tmp <- gsub("[^0-9.:-]","", tmp) 
df <- rbind(df, tmp) 
#Storing t3 
tmp <- data.frame(t(strsplit(as.character(t3), " ")), stringsAsFactors = F) 
colnames(tmp) <- c("V1") 
tmp <- str_split_fixed(tmp$V1, " ", 2) 
tmp <- gsub("[^0-9.:-]","", tmp) 
df <- rbind(df, tmp) 

#Writing the data frame to an Excel file 
write.xlsx(x = df, file = "df.xlsx", sheetName = "Sheet1", row.names = FALSE) 
#reading the data from the Excel file 
readDF <- read.xlsx("df.xlsx", sheetName="Sheet1") 
相關問題