R 数据结构

Vector

Vector 创建


Vector访问


List

List创建


List访问


Matrix

> a
    [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

> class(a)
[1] "matrix"

> attributes(a)
$dim
[1] 3 3

> dim(a)
[1] 3 3

matrix创建

> matrix(1:9, nrow = 3, ncol = 3)
    [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

> matrix(1:9, nrow = 3)
    [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

> matrix(1:9, nrow=3, byrow=TRUE)    
    [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9

> x <- matrix(1:9, nrow = 3, dimnames = list(c("X","Y","Z"), c("A","B","C")))
> x
 A B C
X 1 4 7
Y 2 5 8
Z 3 6 9


> colnames(x)
[1] "A" "B" "C"
> rownames(x)
[1] "X" "Y" "Z"

> # It is also possible to change names
> colnames(x) <- c("C1","C2","C3")
> rownames(x) <- c("R1","R2","R3")

> x
  C1 C2 C3
R1  1  4  7
R2  2  5  8
R3  3  6 

> cbind(c(1,2,3),c(4,5,6))
    [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6

> rbind(c(1,2,3),c(4,5,6))
    [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6

> x <- c(1,2,3,4,5,6)
> x
[1] 1 2 3 4 5 6
> class(x)
[1] "numeric"

> dim(x) <- c(2,3)
> x
    [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6
> class(x)
[1] "matrix"

Matrix访问

整数向量索引

 > x
    [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

> x[c(1,2),c(2,3)]    # select rows 1 & 2 and columns 2 & 3
    [,1] [,2]
[1,]    4    7
[2,]    5    8

> x[c(3,2),]    # leaving column field blank will select entire columns
    [,1] [,2] [,3]
[1,]    3    6    9
[2,]    2    5    8

> x[,]    # leaving row as well as column field blank will select entire matrix
    [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

> x[-1,]    # select all rows except first
    [,1] [,2] [,3]
[1,]    2    5    8
[2,]    3    6    9


> x[1,]
[1] 1 4 7
> class(x[1,])
[1] "integer"

> x[1,,drop=FALSE]  # now the result is a 1X3 matrix rather than a vector
    [,1] [,2] [,3]
[1,]    1    4    7
> class(x[1,,drop=FALSE])
[1] "matrix"

> x
    [,1] [,2] [,3]
[1,]    4    8    3
[2,]    6    0    7
[3,]    1    2    9

> x[1:4]
[1] 4 6 1 8

> x[c(3,5,7)]
[1] 1 0 3

逻辑向量索引

> x
    [,1] [,2] [,3]
[1,]    4    8    3
[2,]    6    0    7
[3,]    1    2    9

> x[c(TRUE,FALSE,TRUE),c(TRUE,TRUE,FALSE)]
    [,1] [,2]
[1,]    4    8
[2,]    1    2

> x[c(TRUE,FALSE),c(2,3)]    
    [,1] [,2]
[1,]    8    3
[2,]    2    9

Data Frame

Data Frame 创建

Data Frame 访问

Factor

> x
[1] single  married married single
Levels: married single

> class(x)
[1] "factor"

> levels(x)
[1] "married" "single"

Factor创建

> x <- factor(c("single", "married", "married", "single")); 
> x 
[1] single  married married single
Levels: married single

> x <- factor(c("single", "married", "married", "single"), levels = c("single", "married", "divorced"));
> x
[1] single  married married single
Levels: single married divorced

> x <- factor(c("single","married","married","single"))
> str(x)
Factor w/ 2 levels "married","single": 2 1 1 2

Factor访问

> x
[1] single  married married single
Levels: married single

> x[3]           # access 3rd element
[1] married
Levels: married single

>  x[c(2, 4)]     # access 2nd and 4th element
[1] married single
Levels: married single

> x[-1]          # access all but 1st element
[1] married married single
Levels: married single

> x[c(TRUE, FALSE, FALSE, TRUE)]  # using logical vector
[1] single single
Levels: married single

Factor修改

> x
[1] single  married married single
Levels: single married divorced

> x[2] <- "divorced"    # modify second element;  x
[1] single   divorced married  single  
Levels: single married divorced

> x[3] <- "widowed"    # cannot assign values outside levels
Warning message:
In `[<-.factor`(`*tmp*`, 3, value = "widowed") :
 invalid factor level, NA generated

> x
[1] single   divorced <NA>     single  
Levels: single married divorced
> levels(x) <- c(levels(x), "widowed")    # add new level

> x[3] <- "widowed"

> x
[1] single   divorced widowed  single  
Levels: single married divorced widowed
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值