Web EndMemo

R Data Frame Data Type

R data.frame is a powerful data type, especially when processing table (.csv). It can store the data as row and columns according to the table. The difference between data frame and matrix is that the column data of matrix are the same, while the column data of data frame may be of different modes and attributes.

Let's use the R Data Sets BOD (Biochemical Oxygen Demand), which is a data frame:
>x <- BOD
[1] TURE
[1] "data.frame"
  Time demand
1    1    8.3
2    2   10.3
3    3   19.0
4    4   16.0
5    5   15.6
6    7   19.8

as.data.frame() can coerce a list into a data frame, providing that the components of the list conforms to the restrictions of a data frame.

Each row of the data frame is a list or a data frame with one row:
>y <- x[2,]
[1] TRUE
[1] TRUE

Access the column of the data frame:
[1] 1 2 3 4 5 7
[1]  8.3 10.3 19.0 16.0 15.6 19.8

A convenient way to access the columns of a data frame is using attach(), detach() statement. e.g. after attach(x), the column x$demand can be accessed by simply typing demand.

[1]  8.3 10.3 19.0 16.0 15.6 19.8

In other words, attach() statement makes the components of the data frame visible. We can do some operations with the variable demand, and the components demand of the data frame will not be changed.

>demand <- demand + 10
[1] 18.3 20.3 29.0 26.0 25.6 29.8
[1]  8.3 10.3 19.0 16.0 15.6 19.8

Statement detach() is the reverse statement of attach().

Error: object 'demand' not found

data.frame is the default data type when you read in a table. Following is a csv table file dataframe.csv, there are "Expression" value vs Subtype "A", "B" and "C" in column 1 and column 2:

Let's read in the data from the file:
>x <- read.csv("dataframe.csv",header=T,sep="\t")
[1] TRUE