How to Upload a .sav File Into R
In this post we are going to larn 1) how to read SPSS (.sav) files in R, and 2) how to write to SPSS (.sav) files using R. More specifically, here we are going to work with the following two R packages haven (from the Tidyverse) and foreign to:
- Read a .sav file into an R dataframe
- Writing an R dataframe to a .sav file
Data Import in R
R is a great statistical programming environment and we can store our information in the .rda format. However, sometimes we may collaborate with other researchers using other statistical software and/or that are storing their data in unlike formats. Luckily, as an R user nosotros can import data from a range of unlike formats; SAS (.7bdat), Stata (.dta), Excel (east.m., .xlsx), and CSV (.csv) to name a few. Here are some tutorials about how to import data from some of these formats:
- How to Import SAS files in R
- How to Read Stata (.dta) Files and Write .dta Files in R
Can R read SPSS files?
In this brief section, we are going to quickly answer the question about Can R read SPSS files? Brusque reply is yep! Here's how:
one)library(oasis)
, and and then
2) read_sav(PATH_TO_YOUR_FILE)
Read the post for more details!
How to Open an SPSS File in R
In this section, we are going to go into more detail on how to load a .sav file into an R dataframe. More specifically, we will outset by using the R bundle haven and, so, go along using the foreign bundle.
Importing a .sav file in R using Haven's read_sav
In this section, we are going to learn how to load an SPSS file in R using the package Haven and the function read_sav.
First, we are going to outset by loading the Haven library:
library(oasis)
Code linguistic communication: R ( r )
In the 2nd pace, nosotros are going to create the dataframe from the SPSS file using the read_sav role. Note that, when nosotros load a file using the Oasis package, it is important to recollect that it will look for the file in R script's working directory. In the read_sav example below, we are going to use an example SPSS file. It tin be downloaded here and should be put information technology in the correct binder (or modify the path in the code chunk below):
df <- read_sav("./SimData/survey_1.sav")
Lawmaking language: R ( r )
In the code chunk to a higher place nosotros create the dataframe df and nosotros can utilize the R role head() to brandish the 10th commencement rows:
head(df, 10)
Code language: R ( r )
Reading an SPSS file from a URL using R
In this next read SPSS file in R example, we are going to read a .sav file from a URL. Here we are going to use this dataset.
df <- read_sav("http://staff.bath.ac.u.k./pssiw/stats2/PsychBike.sav") head(df)
Code language: JavaScript ( javascript )
Reading a .sav file in R using Foreign'due south read.spss
In this section, we are going to apply read.spss from the foreign parcel. More specifically, we are going to read SAV files using read.spss.
Code linguistic communication: R ( r )
df <- read_sav("http://staff.bathroom.ac.uk/pssiw/stats2/PsychBike.sav") head(df)
Now, there are some steps nosotros may demand to go through before analyzing our information. First, we may want to remove a column (or ii columns) in R's dataframe. 2nd, nosotros may need to dummy lawmaking our chiselled variables in R. Finally, we may too want to inspect the data, calculate descriptive statistics, and visualize the data (east.thou., with a besprinkle plot).
Writing R Dataframes to SPSS Files
In this section, we are going to learn how to write dataframes to .sav files. More specifically, we are going to learn how to load CSV and Excel files into R dataframe objects then salve them as SPSS files.
R from CSV to SPSS files
In this subsection, we are going to read a CSV file in R. After we have read this CSV file nosotros are going to save it equally a .sav file.
library(readr) df <- read_csv("./SimData/FirstDayData.csv")
Code linguistic communication: R ( r )
At present that we have imported the CSV file into an R dataframe and we can save it as an SPSS file using the write_sav role from the Oasis package.
library(oasis) write_sav(df, "./SimData/FirstDayData.csv")
Code language: R ( r )
R from Excel to SPSS files
In this subsection, we are going to the exact same as above except that we load an Excel file in R.
library(readxl) df <- read_excel("./SimData/example_concat.xlsx")
Code language: R ( r )
- Learn more well-nigh reading xlsx files in R
At present that we have imported the Excel file into an R dataframe. The final thing we will practice is to save this dataframe to an SPSS file. Once again, we volition be using the write_sav role from the Haven package.
library(haven) write_sav(df, "./SimData/example_concat.sav")
Code language: R ( r )
Conclusion
In this short post, we learned how to read SPSS files into R dataframes. Furthermore, we learned how to write CSV & Excel files to .sav (SPSS) files using R.
Source: https://www.marsja.se/how-to-read-write-spss-files-in-the-r-statistical-environment/
0 Response to "How to Upload a .sav File Into R"
Post a Comment