> ls()
character(0)
> n <- 5
> ls()
[1] "n"
> x <- "acgt"
> ls() ## 查看内存中所有变量名
[1] "n" "x"
> str(n) ## 查看数据结构
num 5
> str(x)
chr "acgt"
> ls.str() ## 查看所有变量的数据类型
n : num 5
x : chr "acgt"
> ls()
[1] "n" "x"
> rm(n) ## 删除变量对象
> ls()
[1] "x"
> rm(list=ls()) ## 删除所有变量对象
向量 (vector)
> x <- c(2, 6.6, 9.6)
> x
[1] 2.0 6.6 9.6
> y <- 2.2:5.2
> y
[1] 2.2 3.2 4.2 5.2
> c(x, y) ## 向量
[1] 2.0 6.6 9.6 2.2 3.2 4.2 5.2
> 1:10
[1] 1 2 3 4 5 6 7 8 9 10
> x <- 1:5
> mode(x) ## 数据结构的一个属性,查看成员数据类型
[1] "numeric"
> length(x) ## 成员个数
[1] 5
> y <- c(FALSE, TRUE)
> y
[1] FALSE TRUE
> mode(y)
[1] "logical"
> length(y)
[1] 2
> 1 > 0
[1] TRUE
> z <- c("order", "family", "genus", "species")
> mode(z)
[1] "character"
> length(z)
[1] 4
> z
[1] "order" "family" "genus" "species"
> z[1:2] ## 索引切片法
[1] "order" "family"
> i <- c(1, 3)
> z[i] ## 索引数值向量法
[1] "order" "genus"
> z[c(1, 1, 1)]
[1] "order" "order" "order"
> z[c(1, 1, 1, 4)]
[1] "order" "order" "order" "species"
> z[-1] ## 切片法移除,所有类似移除的方法都不改变原值
[1] "family" "genus" "species"
> j <- -c(1, 4)
> z[j] ## 数值向量法移除
[1] "family" "genus"
> z[5] ## 索引为空,显示空
[1] NA
> z[-5] ## 移除为空,直接报错
Error: subscript out of bounds
> x[c(1, 4)] <- 10 ## 数值向量法赋值,赋值当然原位修改
> x
[1] 10 2 3 10 5
> z
[1] "order" "family" "genus" "species”
> z[c(TRUE, FALSE)] ## 自动延长等效下一个命令
[1] "order" "genus"
> z[c(TRUE, FALSE, TRUE, FALSE)]
[1] "order" "genus"
> x
[1] 10 2 3 10 5
>x>=5
[1] TRUE FALSE FALSE TRUE TRUE
>x[x >= 5]
[1] 10 10 5
> x <- 4:1
> names(x) <- z ## names是向量的一个属性,给向量建立新的索引
> x
order family genus species
4 3 2 1
> names(x)
[1] "order" "family" "genus" "species"
> x[c("order", "genus")]
order genus
4 2
> names(x) <- NULL ## 删除这种索引
> x
[1] 4 3 2 1
因子(Factor)
> f <- c("Male", "Male", "Male")
> f
[1] "Male" "Male" "Male"
> f <- factor(f) ## factor 有一个重要的属性levels,表示成员所有可能的取值。
> f
[1] Male Male Male
Levels: Male
> ff <- factor(f, levels = c("Male", "Female")) ## 改变 levels
> ff
[1] Male Male Male
Levels: Male Female
> table(f) ## 利用table函数统计factor里的成员,发挥了factor的优势。
f
Male
3
> table(ff)
ff
Male Female
3 0
矩阵(Matrix)
> matrix(1:9, 3, 3) ## 使用matrix函数生成矩阵
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> x <- 1:9
> dim(x) <- c(3, 3) ## 将行数和列数组成向量赋给向量的dim属性,变成矩阵
> x
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> x[3, 2]
[1] 6
> x[3, ] ## extract the 3rd row
[1] 3 6 9
> x[, 2] ## extract the 2nd column
[1] 4 5 6
> rownames(x) <- c("A", "B", "C") ## 和列表不一样,不能给每一个元素建立姓名索引,但是可以给行和列建立姓名
> colnames(x) <- c("v1", "v2", "v3")
> x
v1 v2 v3
A 1 4 7
B 2 5 8
C 3 6 9
> x[, "v1"]
A B C
1 2 3
> x["A", ]
v1 v2 v3
1 4 7
> x[c("A", "C"), ]
v1 v2 v3
A 1 4 7
C 3 6 9
数据框(data frame)
> DF <- data.frame(z, y = 0:3, 4:1) ## data.frame函数建立数据框
> DF ## 自动生成列名字
z y X4.1
1 order 0 4
2 family 1 3
3 genus 2 2
4 species 3 1
> rownames(DF) ## 查询行名
[1] "1" "2" "3" "4"
> colnames(DF) ## 查询列名
[1] "z" "y" "X4.1"
> data.frame(1:4, 9:10) ## 长度不够循环补充
X1.4 X9.10
1 1 9
2 2 10
3 3 9
4 4 10
> data.frame(1:4, 9:11) ## 只能循环补充整数个
Error in data.frame(1:4, 9:11) :
arguments imply differing number of rows: 4, 3
> DF$y 使用"$"提取一列
[1] 0 1 2 3
列表
> L <- list(z = z, 1:2, DF)
> L
$z
[1] "order" "family" "genus" "species"
[[2]]
[1] 1 2
[[3]]
z y X4.1
1 order 0 4
2 family 1 3
3 genus 2 2
4 species 3 1
> length(L)
[1] 3
> names(L)
[1] "z" "" ""
> L[[1]] ## 提取成向量
[1] "order" "family" "genus" "species"
>L[2] ## 提取成列表
[[1]]
[1] 0 1 2 3
> L$z
[1] "order" "family" "genus" "species"
系统帮助
>help("ls") ## 在已经载入包中搜索
>?ls ## 同上
>help("fastaread", try.all.packages = 'Ture') ## 在所有已经安装的包中搜索
>help.search("tree") ## 在所有已经安装的包中,进行主题搜索
>help.search("tree",rebuild = TRUE) ## 刷新安装包信息,然后在所有已经安装的包中,进行主题搜索
>help.start() ## 生成本地帮助文档
对象保存与载入
>save(x, y, z, file = "xyz.RData")
>load("xyz.RData")
>postscript("name.eps") ## 准备保存图片
>dev.off() ## 执行保存
重复命令
>source("name.R") ## 执行R源文件里的程序
>for (i in 1:10){ ## for 循环
+ if (i == 5) next
+ if (i == 8) break
+ print(i)
}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 6
[1] 7
>apply(X, MARGIN, FUN, ...)
>lapply(x, FUN, ...)
>tapply(X, INDEX, FUN = NULL, ...)
> replicate(5, rnorm(1))
[1] -1.424699824 0.695066367 0.958153028 0.002594864
[5] -0.879007194
> replicate(4, rnorm(3))
[,1] [,2] [,3] [,4]
[1,] 0.7743082 -0.7689951 -0.4332675 1.58177859
[2,] -0.7495421 -0.5846179 -1.0581448 0.03818309
[3,] 0.1632760 0.8818927 0.6218508 -1.37648467