Dataframes

Many types of data can be conveniently stored as dataframes.  A dataframe is simply a matrix or table in which the columns are thought of as variables and the rows are cases.  Unlike a matrix, the columns can have various modes: logical, numeric, character, factor, ordered factor or complex.  Within a column all entries must be of the same mode.

Dataframes can be created using the data.frame(...) function to convert a series of vectors, matrices or other dataframes into columns in a dataframe.  A table of data can be input as a dataframe using the read.table(...) function.  A matrix can be directly changed to a dataframe using as.data.frame(...).

A dataframe is a specialized list.

The names of the variables (ie. columns) in a dataframe can be assigned using the function names(...).  The cases (ie.  rows) with row.names(...).  An example to show the syntax:

mat <- (1:5)%o%(5:1)
mat.df <- as.data.frame(mat)
names(mat.df) <- c("one", "two", "three", "four", "five")
row.names(mat.df) <- c("Ian", "David", "Duncan", "Bruce", "Reg")

Using the attach function one can access the columns in the dataframe by referring simply to the names.  The attach function puts the dataframe on the search path.  The detach function removes it from the search path.