Writing data to a file using
write.csv failes to retain column data types. A better way to save/restore data objects to/from files is by using RDS and RData.
We can write/read a single data object, multiple objects, or an entire workspace. Here’s a simple example:
# save data object to file saveRDS(df, file = 'df.rds') # restore object readRDS(file = 'df.rds') # restore object under a different name new_df <- readRDS(file = 'df.rds') # save multiple data objects to file save(df1, df2, file = 'two_dfs.rda') # restore multiple objects load(file = 'two_dfs.rda')
Note: *.rda = *.RData
You can use either file extension with
save(df1, df2, file = 'two_dfs.RData').
For more on RDS and RData, including saving an entire workspace, see: http://www.sthda.com/english/wiki/saving-data-into-r-data-format-rds-and-rdata