Data Slice in R

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值