1. Data Frame
A data frame is a combination of different vectors.
> x = c(1, 2, 3)
> y = c("a", "b", "c")
> z = c(TRUE, FALSE, TRUE)
> co = data.frame(x, y, z)
2. Column Slice
Numeric Indexing
> co[2]
y
1 a
2 b
3 c
Name Indexing
> co["y"]
y
1 a
2 b
3 c
3. Row Slice
Numeric Indexing
> co[2,]
x y z
2 2 b FALSE
To retrieve more than one rows at one time
> co[c(2,1),]
x y z
2 2 b FALSE
1 1 a TRUE
Name Index
Row Slice also could be retrieved by name indexing.
Logical Indexing
> L
[1] FALSE TRUE FALSE
> co[L,"x"]
[1] 2
4. Subset
subset() function could return subsets of vectors, matrices or data frames which meet conditions.
> co1 <- subset(co, select = y)
> co1
y
1 a
2 b
3 c
select
indicating columns to select in a data frame
> co2 <- subset(co, x > 1, select = y)
> co2
y
2 b
3 c
subset logical expression indicating elements or row to keep.
5. 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.
> NROW(co$x)
[1] 3
> nrow(co)
[1] 3