library(dplyr)
# Create a simple tbl_df
v1 = c(1,2,3)
v2 = c("a", "b", "c")
tdf = tbl_df(data.frame("val" = v1, "name" = v2))
save() & load()Using save() in this way also saves the name of the data structure. So when you load it, the name of the original data frame is used.
Note that we are able to save and load a tbl_df without having to convert to data.frame
May be used for saving multiple objects
save(tdf, file="~/Documents/temp/tdf.Rda")
# Delete the tbl_df
rm(tdf)
# Re-instantiate it via load, tdf is reloaded into the environment
load("~/Documents/temp/tdf.Rda")
# another variable to save
x = 10
save(tdf, x, file="~/Documents/temp/tdf.Rda")
# remove the variables from the environment
rm(tdf)
rm(x)
exists("tdf") # FALSE
exists("x") # FALSE
load("~/Documents/temp/tdf.Rda")
exists("tdf") # TRUE
exists("x") # TRUE
saveRDS() & readRDS()Saves only a single object
Assign a variable name to the result when loading
saveRDS(tdf, file="~/Documents/temp/tdf.Rda")
rm(tdf)
# need to assign result to a variable
tdf = readRDS("~/Documents/temp/tdf.Rda")
exists("tdf") # TRUE
write.table()tab-delimited is probably safer than CSV
Don’t forget to do row.names = False or else won’t load correctly into another program (Excel, Tableau, etc)
write.table(tdf, "~/Documents/temp/tabbed.txt", sep="\t", row.names = FALSE)