将数据从Excel加载到R – 第一列中出现的额外date?

在我尝试从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表格中。 其余的是正确的。

我在文档中find了这个段落:

强制从数字转换为date时间:由于Excel将date/时间理解为具有一些附加格式的数字,因此从数字到date时间的转换实际上是可能的。 在这种情况下,数字代表自1900-01-00以来的天数(是,第00天! – 请参阅http://www.cpearson.com/excel/datetime.htm )。 请注意,在R 0中表示为1899-12-31,因为没有1900-01-00。 分数天代表小时,分钟和秒。

这似乎为这个问题提供了一些启示。

以下是如何将数据框保存到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")