A data frame is a combination of different vectors.
1 2 3 4
x = c(1, 2, 3) y = c("a", "b", "c") z = c(TRUE, FALSE, TRUE) co = data.frame(x, y, z)
Column Slice
Numeric Indexing
1 2 3 4 5
> co[2] y 1 a 2 b 3 c
Name Indexing
1 2 3 4 5
> co["y"] y 1 a 2 b 3 c
Row Slice
Numeric Indexing
1 2 3
> co[2,] x y z 22 b FALSE
To retrieve more than one rows at one time
1 2 3 4
> co[c(2,1),] x y z 22 b FALSE 11 a TRUE
Name Index
Row Slice also could be retrieved by name indexing.
Logical Indexing
1 2 3 4
> L [1] FALSETRUEFALSE > co[L,"x"] [1] 2
Subset
subset() function could return subsets of vectors, matrices or data frames which meet conditions.
1 2 3 4 5 6
> co1 <- subset(co, select = y) > co1 y 1 a 2 b 3 c
Select indicating columns to select in a data frame
1 2 3 4 5
> co2 <- subset(co, x > 1, select = y) > co2 y 2 b 3 c
subset logical expression indicating elements or row to keep
Statistic of a data set
To get the number of row and columns, nrow(), ncol(), NROW(), NCOL() could be used. nrow() and ncol() could count for vector, array or data frame, NROW()NCOL() count for 1-column matrix.