Importing Data Sets into R Studio
Data sets that are saved on your local computer can be imported into R. If the data set is posted on a website, first save it to your local computer and then import it.
The Traditional Way (harder)
The older method for importing data sets in to use the read.csv(file.choose()) command. If I were to enter the line of code below into the Console window at the lower right, without a file name in the inner parentheses and then hit the Enter key, it would open a dialog box that allowed me to locate the file on my local computer.
Note that the window may not fully open, but might appear on your taskbar at the bottom of your screen if you are using a PC (see the brown highlghted selection on my taskbar in the image below.
If you click on the highlighted selection on the taskbar, it will open the dialog box in the previous image, and you can browse to locate and select the data file.
Once you double click on the data file to select it, it will import the data set and give it the nickname "fram" in this case, a nickname that I chose just to allow less typing when referring to the data set. Then, to view the data set in the upper left window, you can use the command View(fram), noting that this particular commaned uses an uppercase "V", although most are lowercase.
The Easy Way to Import Data
The easiest way is to click on the "Import Data set" button in the upper right window of R Studio. A pop-down menu will open. If you are importing a .CSV file, choose the first option (From text(base)).
This will open another window enabling you to browse your computer to locate the file you want to import. Double click on the file, and another window will open as shown below.
Does the Data Have Headings?
On the left side there is an option to indicate whether the data set has Headings (variable names at the top of the columns). All of the PH717 data sets have headings, so make sure "Yes" is selected. Then click on the "Import" button at the bottom of this window.
Viewing the Data Set
When the data is imported, R should show the data in the upper left window of the R Studio, and you can scroll right and left and up and down to check the data set for errors. Notice also that R has created a tab just below the main menu with the title for the data set (framstudy in this case).
The data set is also now listed in the window at the upper right and indicates that it has 49 observations and 6 variables.
You can review the data set at any time by clicking on the tab with its name in the upper left window or by clicking on the data set name in the upper right window. You can also review it using the "View()" command in R, e.g., View(framstudy) and then hitting the "Run" button in the upper left window. Note that this R command uses an uppercase "V", although most commands use lower case.
Problems Importing Data Sets
- Importing .CSV files using a PC is generally not a problem, although occasionally there are problems if one is using Chrome as their browser. If so, try using Safari or Firefox.
- Students using Macs sometimes have difficulties that vary with the age and type of computer they are using. When downloading a data set to a Mac, sometimes the extension for the data file gets changed to .txt or another file type. Make sure that the saved file has a .CSV extension. Some of these download problems are solved by downloading using Chrome as your browser instead of Safari.
- Another option is to download the data file as an .XLSX file, and then import it as an XLSX file into RStudio (third option on the Import dataset drop down menu).
- In rare cases, I have had to email data sets to students as .CSV files. Once they save them on their local computer, they have been able to import them into R.
Importing Data from Excel Spreadsheets (.XLSX files) on a PC or Mac
All of the data sets for PH717 are .CSV files, but you can import data from .XLSX files. Once again, use the "Import Dataset" button in the upper right window in R Studio, but select the third option (From Excel).
This will open a window that enables you to browse for the data file you want. Once you locate it, click to select it and then click on the "Import" button at the bottom.