0

R noob here, I just set up RStudio and imported some .sav SPSS datasets into R Environment. But whenever I try to run tests I get an error saying the file does not exist in the current working directory. I have set the directory to desktop and even tried setting it to the exact folder my file is in and still nothing works. I have spent hours on youtube and I keep getting error after error; I even tried using ChatGPT to give me some basic code. Most of the time when I run a script it is just repeating the syntax in the console + giving me some errors. Please help, I am psychology grad student and I am sick of doing statistics on IBM products

I am hopeless and just about ready to give up on R

  • 1
    Please show us some code you are trying, how can we know what you are doing wrong without that? See https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – kjetil b halvorsen Mar 17 '23 at 03:04
  • as the user above said give an example of what you did so we can help but it should be something like this: ```setwd("C:/Users/USER/Documents") file <- haven::read_sav("TestFile.sav")``` – megmac Mar 17 '23 at 03:08
  • You may want to try `foreign::read.spss` first if it can read your version; haven can really be a pain. – jay.sf Mar 17 '23 at 05:15

2 Answers2

0

This sounds unrelated to SPSS, so the title is likely misleading.

Find out where you are (with getwd()) and what files are in the working directory (with list.files()). When you run getwd() and list.files(), do get the values you expect?

If you're running a nontrivial research project and you have time for ChatGPT, I recommend reading the chapter Workflow: scripts and projects for a better long-term .strategy

wibeasley
  • 5,000
  • 3
  • 34
  • 62
0

You need to import the .sav file into R, which will be stored as a data.frame object in R. Let's see how:

  1. For simplicity, set your working directory to be the folder where your .sav file lives

    setwd("path/to/relevant/folder")

  2. Next, you need access to an R function that can read .sav files. That function is called read_sav() and it lives in the haven package. Assuming you have that package installed (more on that below if you don't), run:

    mydata <- haven::read_sav("filename.sav")

  3. You now have a data.frame object in R's global environment called mydata. This is the object on which you should operate. For example, run:

    summary(mydata)


If you have trouble with step 2, read this:

  • You can check whether you have the haven package installed by running:

    find.package("haven")

  • If that returns the path to a folder on your machine, you're good to go. If it throws an error, then you need to install the package. To do so, run:

    install.packages("haven")

  • Now you're ready to read the .sav file into R:

    mydata <- haven::read_sav("filename.sav")

DanY
  • 5,920
  • 1
  • 13
  • 33