下面介紹如何將數據框保存到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")
我認爲你可以使用'read_excel'從'庫(readxl)' – akrun
看起來像'readWorksheet'有一個'rownames'說法。你嘗試過使用它嗎?根據[文檔](https://cran.r-project.org/web/packages/XLConnect/XLConnect.pdf),「rownames = NULL」將起作用。 –
最好用*答案*回答你的問題(你可能需要等一會兒,然後才允許),而不是編輯你的問題來合併答案。 –