Importing .RData and .rds

  • exam.RData (2 KB)
  • exam.rds (300 B)
  • Once datasets have been saved as R data (.RData or .rds), importing the data becomes easier. We will now reload the exam dataset into the Environment.

    To do this, we first clean up our Environment and remove all objects so that we can see the changes. To do this, we use the rm() function (rm stands for remove). In the function, simply list the objects you want to remove from the Environment, separated by a , (comma). Let’s delete everything!

    rm(
      exam, 
      examcsv, 
      examcsv2, 
      path
    )

    When loading R data, it is much easier as the data is directly in a format that R can read. When loading a single object (.rds file), we use the readRDS() function. Important: With this function, we need to assign the read data to an object so that it is available in the Environment. The advantage is that we can rename the data as we like and are not dependent on the file name.

    exam <- readRDS("./data/exam.rds")

    We could also load an entire environment. This could be, for example, all objects of a session, so what we have just done. This type of data is saved in the .RData format and we simply use the load() function.

    # alternatively entire workspace
    load("./data/exam.RData")

    That’s it, this is how we can import data that is available in global formats or in R-specific formats. Finally, we will now learn about importing SPSS datasets, as unfortunately, a number of organizations still provide their datasets in this format.