Short Question
After formatting a dataframe in R with class information like "Date", "Factor", and etc. how to save the dataframe while preserving the class information. Consequently how to load the information back to a new R Project with preserving the above class information.
Long Question
I have created following dataframe with class information,
names <- c("Adrew", "Vikie", "Penny", "Samantha")
date <- as.Date(c("1984-12-28", "1986-10-18", "1986-11-30", "1987-11-30"))
gender <- c("M", "M", "S", "S")
df <- data.frame(names, date, gender)
df$gender <- factor(df$gender,
levels = c("M", "S"),
labels = c(1,2),
order = FALSE)
and the classes are factor, date, factor
method 01
then write the dataframe in csv format and read it
write.csv(df, "df.csv")
dfcsv <- read.csv("df.csv")
and the daraframe classes are changed
method 02
then tried to write the file using table export method
write.table(df, "df.R")
dftable <- read.table("df.R")
again the data frame classes had been changed
Question
is there a proper method to write or save a data frame with preserving the class information like date, factor, etc information intact, and conversely how to load it back.