How to save data file into .RData?

前端 未结 3 1203
礼貌的吻别
礼貌的吻别 2020-12-02 07:35

I want to save data into an .RData file.

For instance, I\'d like to save into 1.RData with two csv files and some information.

Here

相关标签:
3条回答
  • 2020-12-02 07:43

    Just to add an additional function should you need it. You can include a variable in the named location, for example a date identifier

    date <- yyyymmdd
    save(city, file=paste0("c:\\myuser\\somelocation\\",date,"_RData.Data")
    

    This was you can always keep a check of when it was run

    0 讨论(0)
  • 2020-12-02 07:49

    There are three ways to save objects from your R session:

    Saving all objects in your R session:

    The save.image() function will save all objects currently in your R session:

    save.image(file="1.RData") 
    

    These objects can then be loaded back into a new R session using the load() function:

    load(file="1.RData")
    

    Saving some objects in your R session:

    If you want to save some, but not all objects, you can use the save() function:

    save(city, country, file="1.RData")
    

    Again, these can be reloaded into another R session using the load() function:

    load(file="1.RData") 
    

    Saving a single object

    If you want to save a single object you can use the saveRDS() function:

    saveRDS(city, file="city.rds")
    saveRDS(country, file="country.rds") 
    

    You can load these into your R session using the readRDS() function, but you will need to assign the result into a the desired variable:

    city <- readRDS("city.rds")
    country <- readRDS("country.rds")
    

    But this also means you can give these objects new variable names if needed (i.e. if those variables already exist in your new R session but contain different objects):

    city_list <- readRDS("city.rds")
    country_vector <- readRDS("country.rds")
    
    0 讨论(0)
  • 2020-12-02 07:58

    Alternatively, when you want to save individual R objects, I recommend using saveRDS.

    You can save R objects using saveRDS, then load them into R with a new variable name using readRDS.

    Example:

    # Save the city object
    saveRDS(city, "city.rds")
    
    # ...
    
    # Load the city object as city
    city <- readRDS("city.rds")
    
    # Or with a different name
    city2 <- readRDS("city.rds")
    

    But when you want to save many/all your objects in your workspace, use Manetheran's answer.

    0 讨论(0)
提交回复
热议问题