Data Slice in R

来源:互联网 发布:乐视mac码 编辑:程序博客网 时间:2024/06/01 14:24
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]  y1 a2 b3 c

Name Indexing

> co["y"]  y1 a2 b3 c
3. Row Slice
Numeric Indexing

> co[2,]  x y     z2 2 b FALSE
To retrieve more than one rows at one time

> co[c(2,1),]  x y     z2 2 b FALSE1 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  y1 a2 b3 c
select indicating columns to select in a data frame
> co2 <- subset(co, x > 1, select = y)> co2  y2 b3 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



0 0
原创粉丝点击