Introduction to R
4. Entering data
So far we have entered data directly by typing. Clearly this is very time consuming, and you probably already have your data in some form of electronic format. You can import data directly into R from a variety of formats, including Excel. Things get a bit tricky when you have multiple worksheets and formulas in a Excel. However, once you start using R you will never bother to use Excel for anything more than storing raw data in a single worksheet anyway. The easiest way to enter data is to import it from a comma-delimited file. A comma-delimited (.csv) file is easily produced from an Excel spreadsheet. Just open your Excel spreadsheet in Excel to the worksheet you want to import into R, and Choose Save as -> Comma Separated Values. There is also a fantastic R package called “foreign”, which can read in to R different files from many types of statistical packages including SAS-transport files.
Example Dataset
Download the Recruits.csv file, and save it to your c:\data directory. If you double click on it, it will open in Excel. It contains data from a study of 205 military recruits. Here is what it looks like:
Box 1 below describes the variables:
Variable |
Description |
Coding |
injured |
Injured during training |
0=No, 1=Yes |
gender |
Gender |
1=Male, 2=Female |
age |
Age |
|
histinj |
Any previous lower limb injury? |
0=No, 1=Yes |
smokes |
Smokes cigarettes |
0=No, 1=Yes |
boots |
Type of boots |
0=General Purpose, 1=Taipan, 2=Other |
comfort |
Boot comfort score |
1=Extremely comfortable, 2=Comfortable, 3=OK, 4=Uncomfortable, 5=Extremely uncomfortable |
height |
Height (cm) |
|
weight |
Weight (kg) |
|
vo2max |
Aerobic fitness from initial run |
|
Being a comma-delimited (.csv) file, this file is simple plain text with the rows represented on new lines, and the columns separated by commas within each line. Try opening the Recruits.csv file in NotePad (right click > open with > NotePad).
Download the Recruits.csv file, and save it to your c:\data directory. If you double click on it, it will open in Excel. It contains data from a study of 205 military recruits. Here is what it looks like: