R save and loads dataframes

前端 未结 1 549
渐次进展
渐次进展 2021-01-23 17:38

I saved the data frame using this command save(countrydata,file=\"data.Rda\") and load it using this command load(\"data.Rda\") but I do not see the table with the data I hav

相关标签:
1条回答
  • 2021-01-23 18:00

    Saving a single data.frame with save can be confusing because load adds the data.frame (and any other objects in the file) into the parent.frame() from which load is called. (See Hadley's chapter on environments in case this terminology is confusing.) It does this invisibly (i.e., it doesn't explicitly tell you that the objects have been loaded). You have to look for them explicitly using ls().

    Roman's example (in comment) demonstrates this nicely:

    x <- 1:10
    save(x, file = "test.Rda")
    rm(x)
    load("test.Rda")
    ls()
    ## [1] "x"
    

    You can also avoid this confusion by loading objects into an environment other than the default using the envir argument to load:

    e <- new.env()
    load("test.Rda", envir = e)
    ls()
    ## [1] "e"
    ls(envir = e)
    ## [1] "x"
    

    This keeps it out of your global workspace (and thus prevents load from overwriting existing objects).

    You might also want to look at the saveRDS function, which is designed for saving single objects:

    saveRDS(x, file = "test.Rda")
    

    This will save the object by itself. You can then load it using readRDS, which has a return value equivalent to the object:

    readRDS("test.Rda")
    ## [1]  1  2  3  4  5  6  7  8  9 10
    
    0 讨论(0)
提交回复
热议问题