Part 5 Importing data from a CSV file
Get the data from Laulima and save it to your folder. It is also available here. Our data set contains 10 observations. They are school professionals who responded to a questionnaire that had five Likert-type questions and one yes-no question asking whether or not they are a teacher. Here are the data:
PID | Lik1 | Lik2 | Lik3 | Lik4 | Lik5 | Teacher |
---|---|---|---|---|---|---|
1 | 5 | 5 | 4 | 3 | 2 | No |
2 | 2 | 2 | 3 | 1 | 2 | Yes |
3 | 4 | 4 | 3 | 3 | 2 | No |
4 | 2 | 2 | 2 | 1 | 2 | Yes |
5 | 5 | 5 | 3 | 5 | 4 | No |
6 | 1 | 1 | 2 | 2 | 3 | Yes |
7 | 1 | 2 | 3 | 1 | 1 | Yes |
8 | 4 | 1 | 3 | 4 | 5 | No |
9 | 5 | 3 | 4 | 4 | 3 | No |
10 | 2 | 2 | 3 | 3 | 4 | Yes |
These are made-up data. Let’s say that the Likert-type items asked the school professionals how energetic they felt on a particular Wednesday afternoon. PID is the respondent’s identifier code, which we arbitrarily assigned.9 Lik1 through Lik5 are our Likert-type items, corresponding to the five questions. Teacher is coded Yes for yes, this person is a teacher and No for no, they’re not.
Let’s read the data set into R and save it to an object, which we’ll arbitrarily call dat. The read.csv()
function does just that—it reads CSV files.10 The CSV file needs to be in the same folder as your .R file (or you have set the working directory to that folder, or you have used the directory to the file within quotes). Also, be sure to use quotes around the name. Finally, R is case sensitive, so if you get an error, check the spelling. Type the following code in your source pane in RStudio, place your cursor anywhere on the line, and click the Run
button.
The less-than symbol < and the dash symbol - together as <- are the ‘assign to’ operation in R. We have instructed R to read the CSV file that is in our current directory (or folder) and assign that to an object labeled ‘dat’ in our R session. |
Notice that nothing seems to show up. We don’t see our data. Actually, we did create the object dat
. It’s just that we don’t see it unless we ask for it. So, let’s now examine the object we just created.
If you were unable to download the data, you can also try this code to directly download the file from the web site:
I used Excel to create this CSV file. Excel has this quirky behavior of reformatting CSV files that have
ID
as the first entry in the document’s first cell. For that reason, I usePID
(for person ID) instead ofID
.↩︎CSV files are comma-separated-value files, which we might have saved from a worksheet in a spreadsheet program such as Excel.↩︎