STA3050 Lec1笔记
- seq
- rep
- matrix
- list
- as.numeric
- with
- factor
- is.na
#在R中<-和=等价,但<-是全局赋值操作,=是局部赋值操作
seq&rep函数
seq(1, 21, by = 4)
[1] 1 5 9 13 17 21
rep(3, 5) #重复3五次
[1] 3 3 3 3 3
rep(c(1, 4), c(2, 3)) #分别重复“1,4”二、三次
[1] 1 1 4 4 4
rep(1:5, each=2)
[1] 1 1 2 2 3 3 4 4 5 5
rep(1:5, rep(2, 5))*#这里的rep(2, 5)和each=2一样*
[1] 1 1 2 2 3 3 4 4 5 5
rep(1:4,each=2,times=1:8)
[1] 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
#times:each后的向量的处理,如果times是单个值,则each后的值整体重复times次数,如果是x each后的向量相等长度的向量,则对each后的每个元素重复times同一位置的元素的次数
#即原本[1] 1 1 2 2 3 3 4 4 5 5中的第一个数值1重复1次,第二个数值1重复2次,第三个数值2重复3次,第四个数值2重复4次……
rep(1:4,each=2,times=1:8,len=5)#忽视times函数,取前5位(len=5)
[1] 1 1 2 2 3
Matrix函数
> m<-matrix(1:12,nrow=3,ncol=4) #按colunm排序
> m
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> m<-matrix(1:12,nrow=3,byrow=T) #按row排序,这时候T可为任何数。T的含义是True,如过byrow=T改成byrow=F则按colunm排序。byrow=T可以简写为T,byrow=F可以简写为F,nrow=3可以简写为3,如m<-matrix(1:12,nrow=3,ncol=4,byrow=T)可以简写为m<-matrix(1:12,3,4,T)
> m
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
> m[-2,]#去除第二行
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 3 6 9 12
> m[c(1,2),c(3,2,4)]
[,1] [,2] [,3]
[1,] 7 4 10
[2,] 8 5 11
> matrix(1:10, nrow=3)#会报错,数据长度[10]不是矩阵行数[3]的整倍,故增加1,2
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 1
[3,] 3 6 9 2
#Combine two matrices:
column‐wise (cbind) or row‐ wise (rbind)
e.g. > rbind(m,m1) or > cbind(m,m2)
#Multidimensional Arrays:
> arr <- array(data=1:24,dim=c(3,4,2))
> arr
, , 1 #第一层
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
, , 2 #第二层
[,1] [,2] [,3] [,4]
[1,] 13 16 19 22
[2,] 14 17 20 23
[3,] 15 18 21 24
list函数
> x<-c(1:3)
> y<-c(“a”,“b”)
> z<-matrix(1:6,nrow=2)
> w<-list(x,y,z)
>w
[[1]]: #the first component of w
[1] 1 2 3
[[2]]:
[1] “a” “b”
[[3]]:
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
#list可以命名e.g. names(w)<-c(“x”,“y”,“z”)
> w$x #$用于提取其中某个变量结果
[1] 1 2 3
> u<-unlist(w) #把w改为向量
[1] “1" "2" "3" "a" "b" "1" "2" "3" "4" "5" "6“
> as.numeric(u) #把u变成数列,a、b变成了NA
[1] 1 2 3 NA NA 1 2 3 4 5 6
Data Frame
各个成员为等长向量
With函数
#无需$,直接调用数据,并且可以添加运算
e.g. with(women, weight/height) #调用women数据中的weight和height并输出weight/height的值
factor函数
Levels就是factor中的所有元素的集合(没有重复)。我们可以发现Levels就是factor中元素排除重复后且字符化的结果。
> grp <- c("control", "treatment", "control", "treatment")
> grp <- factor(grp)
> grp
[1] control treatment control treatment
Levels: control treatment
> as.integer(grp)
[1] 1 2 1 2
> levels(grp)
[1] "control" "treatment"
> levels(grp)[as.integer(grp)]
[1] "control" "treatment" "control" "treatment"
> levels(grp) <- c("1","2") > grp #改factors
[1] 1 2 1 2
Levels: 1 2
#所有的factor是通过字符串的形式储存的
#==是判断符号,输出为True/False
R 只能储存 53 bits 的数值,即1.XXX 最多52个X
R是2进制,故有时候计算结果有偏差
is.na() 可以检验数列中每个数值是否为NA,输出结果为True/False的字符串
>is.na(some.evens)
[1] TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE
! 是Not的意思
e.g.
!is.na(some.evens)
[1] FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE
可以只展示非NA的数值,如下
> some.evens[!is.na(some.evens)]
[1] 2 4 6 8 10 12 14 16 18 20