timerring

Data Export

February 25, 2024 · 3 min read
Tutorial
R
If you have any questions, feel free to comment below. Click the block can copy the code.
And if you think it's helpful to you, just click on the ads which can support this site. Thanks!

1. Export data #

Since R is mainly used for data analysis, import files are more commonly used than export files, but sometimes we also need to export data or analysis results. The functions write.table( ) and write.csv( ) can export data to a .txt file and a .csv file, respectively.

In addition, the function save( ) can save the specified object in the workspace as an R data file with the extension .rdata. For example:

ID <- 1:5
sex <- c("male", "female", "male", "female", "male")
age <- c(25, 34, 38, 28, 52)
pain <- c(1, 3, 2, 2, 3)      
pain.f <- factor(pain, levels = 1:3, labels = c("mild", "medium", "severe"))   
patients <- data.frame(ID, sex, age, pain.f)
save(patients, file = "patients.rdata")

# IMPORT DATA
load("patients.rdata")

the rdata format file takes up less space and loads quickly with R. Therefore, it is recommended that users ** save data in rdata format ** after importing and organizing data in other formats. To import data in this format, simply call the load( ) function.

2. Importing and exporting data with rio packages #

The rio package in R aims to provide a package similar to a universal tool, and simplifies the work of users importing and exporting data with a unified import( ) function and export( ) function. In addition, the convert( ) function in the package enables conversion between different file formats. The rio package supports a variety of file formats, including data file formats used in SAS, SPSS, Stata, Excel, MATLAB, Minitab, and other software.

The following describes several common functions of the rio package using the dataset infert in the datasets package as an example.

When you load the rio package locally, you can run the install_formats( ) command to install if R prompts that some of the recommended packages are not installed.

library(rio)
data("infert")
str(infert)
# 'data.frame':	248 obs. of  8 variables:
# $ education     : Factor w/ 3 levels "0-5yrs","6-11yrs",..: 1 1 1 1 2 2 2 2 2 2 ...
# $ age           : num  26 42 39 34 35 36 23 32 21 28 ...
# $ parity        : num  6 1 6 4 3 4 1 2 1 2 ...
# $ induced       : num  1 1 2 2 1 2 0 0 0 0 ...
# $ case          : num  1 1 1 1 1 1 1 1 1 1 ...
# $ spontaneous   : num  2 0 0 0 1 1 0 0 1 0 ...
# $ stratum       : int  1 2 3 4 5 6 7 8 9 10 ...
# $ pooled.stratum: num  3 1 4 2 32 36 6 22 5 19 ...

The function str( ) is often used to see the size of the dataset (number of records and number of variables observed), as well as the type of each variable. As you can see from the output above, infert is a data frame with 248 observation records, each with 8 variables.

Run the following command to export this data frame as a .csv file:

export(infert, "infert.csv")

The data file named infert.csv that was just exported can be found in the current working directory. Run the following command to convert the file from a .csv file to a .sav file:

convert("infert.csv", "infert.sav")

Then use the function import( ) to import the .sav file generated above into R and name it infert.data:

infert.data <- import("infert.sav")

Comparing the original infert dataset with the imported infert.data dataset, there is no difference except that the type of the first variable education is different. In R, the as series function can be used to implement the conversion of variable types.

For example, here the character-based variable education in the data frame infert.data is converted to a factor:

infert.data$education <- as.factor(infert.data$education)
str(infert.data$education)
# Factor w/ 3 levels "0-5 yrs","12+ yrs",..: 1 1 1 1 3 3 3 3 3 3 ...

Related readings


<< prev | Strategies for... Continue strolling My Must Haves... | next >>

If you want to follow my updates, or have a coffee chat with me, feel free to connect with me: