Once you’ve completed the first step of using R, downloading the program, you need to figure out the second step, opening your data in R. There are perhaps dozens of ways to open your data in R, but the current page is a brief guide on one of the easiest ways: opening your data as a .csv file. If you have any questions or comments about the current guide, please email me at MHoward@SouthAlabama.edu. I am always happy to help.
As mentioned above, the current guide provides instructions on opening a .csv file in R. If you typically handle your data in Excel, however, your data is probably in a .xls or .xlsx format. Fortunately, changing your data from .xls or .xlsx format to .csv is extremely easy to do (if you have Excel!). So, the current guide begins with changing your data from .xls or .xlsx to .csv.
To do so, first open your data in Excel. Then, go to File, Save As, and double-click This PC.
Now, choose the folder to which you want to save your file. Before clicking “Save,” however, you need to click on the dropdown menu beside the text that reads, “Save as Type.”
From this menu, scroll down and click on CSV (Comma delimited).
And then finally click on save.
Great! We should now have a .csv file that is ready to be read into R! To open this file in R, we need to open R. Once R is open, we need to start a new script. To do so, go to File, and then click on New Script (as seen below).
Now, you should have a new window. You can think of these as separate input and output windows. The new window is the information that you input into R, whereas the other window is the information that R outputs to you. To do almost anything in R, you need to write syntax in this input window, which is similar to writing computer code. Don’t worry! It isn’t that bad once you get used to it.
The syntax that we are going to write is to open our .csv file. To do so, we first want to type the following in our input window: MyData <- .
Why do we do this? Because R can be considered an object-oriented programming language. Whenever we import data or run analyses, we have to associate it with a word. In the current example, we are going to associate our data with the word MyData. So, once our data is imported, we can just type MyData, and R will know that we are referring to our dataset. Neat, isn’t it?
To continue, we then need to type: read.csv(file= .
This is our command. Read.csv is preprogrammed into R, and it can automatically import our data. . .if we tell the command where our data is located. So, the next step is to type in the location of our data.
As you may already know, each file on a computer has its own directory path, which is how computers can locate our files. You can think of it as an address, and each file has its own address. So, we need to tell R the address of our file. To get the address of our file, the easiest way is to probably open the folder containing our file. Then, hold down Shift and right-click your file. Then, click on copy-as-path as seen below:
Now, you can just right click and paste into R, and it should look something like what we have below:
Before continuing, we need to turn the backslashes into forwardslashes. R won’t read your data otherwise.
There are three very last things you need to add. You need to specify whether your .csv file has a header (i.e. variable names in the first row). If it does, you should add: , header=TRUE . If it does not, you should add: , header=FALSE .
Then, you need to add: , sep=”,” . This just tells R that your data is separated by commas, because you are reading a comma-separated value (.csv) file.
Lastly, you need to close your parenthesis by adding: ) . Finally, you should have something like this:
Highlight all your syntax, right click, and then left click on “Run line or selection”. Keep your fingers crossed!
NOTE: Those using Mac versions of R may instead have to run the Execute command. I believe this can be found in the Menu bar (the options at the top of the screen) of R, but you may be able to find Execute by right-clicking. Either way, keep your fingers crossed!
Did the result look something like the picture above? Sort of anticlimactic, huh? Well, in our output window, type in MyData (remember the capitalizations) and press enter.
Woah! It should show all your data to you. If you followed the instructions correctly (and assuming that I typed them in correctly), R should now have your data associated with the word MyData. Now, you can progress to performing analyses with your data in R. As always, if you have any questions, comments, or concerns, please email me a MHoward@SouthAlabama.edu.