How to read data in R

This is probably the hardest step in R, so I’ll just write how to do it here. It is possible to read MS Excel files in R, but I find it much easier to read .csv files or tab-delimited files (saved as .txt). To save a spread sheet as .csv (comma-separated) or as tab-delimited .txt, select “Save as…” in your spreadsheet editor (probably Excel).

To read a .csv file in R, use:

mydata <- read.csv("FILE_LOCATION/FILE_NAME.csv")

To read a tab-delimited .txt file in R, use:

mydata <- read.delim("FILE_LOCATION/FILE_NAME.txt")

Where mydata is a name of your choice, while FILE_LOCATION and FILE_NAME must be those of the file where your data are stored. To avoid having to type FILE_LOCATION and FILE_NAME by hand, you can use this trick:

mydata <- read.csv(file.choose())

mydata <- read.delim(file.choose())

(The two lines of code above jam my Mac but they work fine in Windows).