2.2) Importing Data Frames
Data are normally imported as data frames. A data frame is a type of list where all the elements are vectors of the same length: i.e. they can be viewed as columns in a table.
There are various ways to import data from a spreadsheet into an R data frame. The best one may be to save your data (e.g. from Excel) as a comma-separated file (CSV format) and use read.csv("File.csv")
(where File.csv is the name of the file). For this to work, you need to save the file in the directory where R is running (check this with getwd()
).
Sometimes CSV files are exported with extra empty rows at the end. Try deleting these in Excel and re-exporting – or you can delete them in R (see Section 4). An alternative is to save Excel files in text format (e.g. "File.txt") and use read.table("File.txt", header=TRUE)
to import them. This works so long as any missing data are entered as NA, and there must be no spaces in any text entries.