library(haven)
library(readr)
library(writexl)
library(tidyverse)
library(gt)
Upon completing data cleaning and pre-processing, the subsequent step involves exporting the DataFrame to a file. In this demonstration, we showcase the simplicity of exporting by covering the following methods:
1.5.1 Export to CSV
1.5.2 Export to Excel
1.5.3 Export to Stata
1.5.4 Export to SPSS
This practical guide will equip you with the skills to export your processed data to various file formats, ensuring accessibility and compatibility for future analyses or sharing.
Utilize the write_csv() function from the
readr library to export a DataFrame from R to
a CSV formatted file.
df = mtcars
write_csv(x = df, file = "K:/data1.csv")
The df data frame will be saved on local drive
K.
In this Section, we show how to following:
1.5.2.1 Export a single DataFrame
1.5.2.2 Export multiple DataFrames to separate Excel worksheet
1.5.2.1 Export a single DataFrame
write_xlsx(
x = df,
path = "K:/data2.xlsx"
)
The df data frame will be saved on local drive
K.
1.5.2.2 Export multiple DataFrames to separate Excel worksheet
Load a dataset set to use for illustration.
df = readRDS(file = "~/STEMResearch/datasets/hh_income.RDS")
gt(df)
| id | race | gender | married | education | age | income | expenditure | ses |
|---|---|---|---|---|---|---|---|---|
| 1 | White | Female | Yes | Masters | 32 | 528.87 | 417.77 | High |
| 2 | Other | Male | Yes | Bachelors | 46 | 422.02 | 501.42 | High |
| 3 | White | Female | No | Doctoral | 43 | 466.81 | 336.07 | Middle |
| 4 | Black | Female | No | Masters | 35 | 598.32 | 454.08 | Low |
| 5 | Black | Male | Yes | Bachelors | 36 | 510.16 | 340.33 | Middle |
| 6 | Other | Female | Yes | Doctoral | 44 | 517.48 | 372.84 | Middle |
| 7 | White | Male | Yes | Bachelors | 30 | 399.02 | 348.82 | Low |
| 8 | Black | Male | No | Bachelors | 42 | 546.79 | 393.82 | Low |
| 9 | Other | Female | Yes | Masters | 30 | 467.69 | 460.62 | Low |
| 10 | Black | Male | Yes | Doctoral | 28 | 551.13 | 353.95 | High |
Let us begin by creating two different subset data frames of
female and male respondents from the above
df dataframe.
df_female <- df %>%
filter(gender == "Female")
gt(df_female)
| id | race | gender | married | education | age | income | expenditure | ses |
|---|---|---|---|---|---|---|---|---|
| 1 | White | Female | Yes | Masters | 32 | 528.87 | 417.77 | High |
| 3 | White | Female | No | Doctoral | 43 | 466.81 | 336.07 | Middle |
| 4 | Black | Female | No | Masters | 35 | 598.32 | 454.08 | Low |
| 6 | Other | Female | Yes | Doctoral | 44 | 517.48 | 372.84 | Middle |
| 9 | Other | Female | Yes | Masters | 30 | 467.69 | 460.62 | Low |
df_male <- df %>%
filter(gender == "Male")
gt(df_male)
| id | race | gender | married | education | age | income | expenditure | ses |
|---|---|---|---|---|---|---|---|---|
| 2 | Other | Male | Yes | Bachelors | 46 | 422.02 | 501.42 | High |
| 5 | Black | Male | Yes | Bachelors | 36 | 510.16 | 340.33 | Middle |
| 7 | White | Male | Yes | Bachelors | 30 | 399.02 | 348.82 | Low |
| 8 | Black | Male | No | Bachelors | 42 | 546.79 | 393.82 | Low |
| 10 | Black | Male | Yes | Doctoral | 28 | 551.13 | 353.95 | High |
Make a list of the 3 DataFrames df,
df_female, df_male created above.
dfs <- list(
"All" = df,
"Female" = df_female,
"Male" = df_male
)
Now export the DataFrames specified in the dfs list
above
write_xlsx(
x = dfs,
path = "K:/hh_income3.xlsx"
)
The three data frames df, df_female and
df_male will be saved on local drive K. Each
of these data frames will appear as different worksheets of the
hh_income3.xlsx workbook .
Efficiently exporting DataFrames to Stata is accomplished by
utilizing the write_dta() function available in
the haven library.
write_dta(
data = df,
path = "K:/hh_income.dta"
)
The df data frame will be saved on local drive
K.
In addition to its functionality for Stata export, the
haven library includes the write_sav()
function, specifically designed for exporting DataFrames to
SPSS format.
write_sav(
data = df,
path = "K:/hh_income.sav"
)
The df data frame will be saved on local drive
K.