Is there a way to save weighted data so next time I directly load it? I have a large survey dataset, and use R's survey
package. I load the data with feather
package, but it takes considerable time when I apply svydesign. Here is a reproducible example:
df <- data.frame(col1 = rnorm(20, 0, 1), col2 = rnorm(20, 2, 2), w = rnorm(20, 1, .2))
df.w <- svydesign(id = ~1, data = df, weights = ~w)
I want to save df.w and use this for future analysis. Is there a way?
You can use saveRDS
and readRDS
to save/read single R
objects like this.
library(survey)
df <- data.frame(col1 = rnorm(20, 0, 1), col2 = rnorm(20, 2, 2), w = rnorm(20, 1, .2))
df.w <- svydesign(id = ~1, data = df, weights = ~w)
####### save to file ##########
storage_file <- tempfile()
#storage_file <- "mydesign.rds" ## uncomment here to use a local file
saveRDS(df.w, storage_file)
######## clear workspace #########
rm(df, df.w)
######### load the data ###########
df.w.loaded <- readRDS(storage_file)
df.w.loaded
## Independent Sampling design (with replacement)
## svydesign(id = ~1, data = df, weights = ~w)
######## delete storage file ######
file.remove(storage_file)
If you want to save several objects in a single file, take a look at ?save
and ?load
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.