Learn how to install the foreign package in R.
- [Lecturer] R can read in basic formats without any problem, txt and csv. But you need the foreign package added to R in order to read in foreign data types, such as the ones that come from SAS and SPSS. BRFSS is in SAS.xpt format, so we'll need the foreign package. Now, you don't need to go look up the documentation on the foreign package to load it, but let's look up the information about foreign anyway, just so you can see what the documentation looks like.
So here is the documentation available on CRAN about R. As you can see, the foreign package was designed for reading in data of different formats from different statistical programs, such as SAS and SPSS into R. Isn't that nice? But before we can use this package, we need to download it, so let's go back to R and do that from within R. Here, we are going to install the foreign package. Let's go to Packages, Install packages.
Here we need to choose a server that's going to serve up our package. I'm always California Dreaming, so I'm going to pick California. And now we have to find foreign on this list. Here it is. Okay, as you can see in the console, we loaded the package. Now let's look at some code that uses it to read in our data file. Loading the package wrote a lot of text to our console. What a mess.
Let's clear the console by right-clicking on the window and choosing Clear window. Phew, much better. Let's turn to our code. You'll notice that I'm very careful about my code, I comment about it constantly, using an octothorpe or a pound sign for those of you who are less formal, and then writing my note. You'll also see that I use line breaks to keep the code neat and give it some breathing room, spread out and use all the space you want, no reason to crowd yourself. Let's start at the top.
Even though the package is loaded, we have to tell R to load it into memory before we use it and that's with the library function. So we do library(foreign) to load it. Let's run that line of code. Great, okay, now we are ready to download our BRFSS data and read it in. So, in this section, I taught you how to look up documentation on R packages, such as the foreign package. Next, I showed you how to go into R and install the package.
And finally, how to call the package out in code when you use it before you use commands in it, otherwise, R won't recognize the commands unless it knows they are from a package. Because R is free and open source, base R is very lean, you find yourself installing a lot of packages over the course of your analysis, whenever you want to do something special. Now that we've installed the foreign package, in the next section I'll show you how to download and read in the BRFSS dataset.
This detailed, practical course is designed to help those in the field of public health, medicine, and data science to edit, analyze, and interpret data. Learn how to code new variables, use the forward-stepwise modeling process, and document your decisions. Find out how to visualize results by generating charts and graphics, and how to add tables and figures to your documentation. This course helps equip you to independently design, develop, and execute a full BRFSS analysis, and even publish your results in scientific publications or journals.
- Reviewing survey data and documentation
- Conducting a BRFSS analysis
- Understanding naming conventions
- Editing variables
- Reviewing distributions
- Generating an analytic dataset
- Developing descriptive statistics to answer prespecified hypotheses
- Preparing publication-worthy tables and plots