Day1-R 01-get_start

2.1.向量生成

(1)用 c()逐一放到一起,允许重复值

> c(2,5,6,2,9) 
[1] 2 5 6 2 9
> c("a","f","md","b")
[1] "a"  "f"  "md" "b" 

(2)连续的数字用冒号“:”

> 1:15
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15

(3)有重复的用rep(),有规律的序列用seq(),等差数列随机数用rnorm

> rep("gene",times=15)  
 [1] "gene" "gene" "gene" "gene" "gene" "gene" "gene" "gene" "gene"
[10] "gene" "gene" "gene" "gene" "gene" "gene"
> seq(from = 3,to = 21,by = 3)#等差数列
[1]  3  6  9 12 15 18 21
> rnorm(n = 5, mean = 3, sd = 5)#正态曲线:平均值+-标准差
[1]  7.564325 -2.151259  5.991248  2.028028  7.973115
 #(4)通过组合,产生更为复杂的向量。
> paste0(rep("gene",times=15),1:15)#两个向量一对一连接
 [1] "gene1"  "gene2"  "gene3"  "gene4"  "gene5"  "gene6"  "gene7" 
 [8] "gene8"  "gene9"  "gene10" "gene11" "gene12" "gene13" "gene14"
[15] "gene15"

练习2: 向量生成

1.将两种不同类型的数据用c()组合在一起,看输出结果


> c(1,"a")
[1] "1" "a"
> c(T,F,0)
[1] 1 0 0
> c(T,"a")
[1] "TRUE" "a"   

2.生成1到30之间所有4的倍数,答案是


  #4,8,12,16,20,24,28
> seq(from = 4,to = 30,by = 4)
[1]  4  8 12 16 20 24 28

3.生成sample4,sample8,sample12…sample28

>  # 提示:paste0
> paste0(rep("sample",times = 7),
+        seq(from = 4,to = 30,by = 4))
[1] "sample4"  "sample8"  "sample12" "sample16" "sample20" "sample24"
[7] "sample28"

2.2对单个向量进行的操作

(1)赋值给一个变量名

> x = c(1,3,5,6,2) #随意的写法
> x
[1] 1 3 5 6 2
> x <- c(1,3,5,3) #规范的赋值符号Alt➕减号
> x#四个元素
[1] 1 3 5 3
> #赋值+输出同步运行 
> x <- c(1,3,5);x#两行命令用分号连接
[1] 1 3 5
> (x <- c(1,3,5))
[1] 1 3 5

(2)简单数学计算

> x+1
[1] 2 4 6
> log(x)
[1] 0.000000 1.098612 1.609438
> sqrt(x)
[1] 1.000000 1.732051 2.236068
> x %% 10 #取整
[1] 1 3 5
> x %/% 3 #取余
[1] 0 1 1

(3)根据某条件进行判断

> x>3
[1] FALSE FALSE  TRUE
> x==3
[1] FALSE  TRUE FALSE

(4)初级统计

> max(x) #最大值
[1] 5
> min(x) #最小值
[1] 1
> range(x) #max和min极差
[1] 1 5
> which.max(x) #最大值下标#返回的是位置(即下标,索引)
[1] 3
> which.min(x) #最小值下标
[1] 1
> sort(x,decreasing = T)
[1] 5 3 1
> mean(x) #均值
[1] 3
> median(x) #中位数
[1] 3
> var(x) #方差
[1] 4
> sd(x) #标准差
[1] 2
> sum(x) #总和
[1] 9
> length(x) #长度?
[1] 3
> unique(x) #去重复
[1] 1 3 5
> table(x) #重复值统计
x
1 3 5 
1 1 1 

2.3.对两个向量进行的操作#####

> x = c(1,3,5)
> y = c(3,2,5)

(1)逻辑比较,生成等长的逻辑向量

> x == y 
[1] FALSE FALSE  TRUE
> x %in% y #x中的每一个元素判断是否在y中,输出逻辑值
[1] FALSE  TRUE  TRUE

(2)数学计算

> x + y#一一相加
[1]  4  5 10

(3)“连接“

> paste(x,y,sep=":")#注意与paste0的区别,是否可以用sep
[1] "1:3" "3:2" "5:5"

(4)交集、并集、差集

> intersect(x,y)#交集
[1] 3 5
> union(x,y)#并集,,自动去重
[1] 1 3 5 2
> setdiff(x,y)#差集,返回前面那个有的,你有我没有,以第一个为准
[1] 1
> #当两个向量长度不一致
> x == y
[1] FALSE FALSE  TRUE

2.4.利用循环补齐简化代码

> paste0(rep("gene",15),1:15)#按tab键,然后选择下一类
 [1] "gene1"  "gene2"  "gene3"  "gene4"  "gene5"  "gene6"  "gene7" 
 [8] "gene8"  "gene9"  "gene10" "gene11" "gene12" "gene13" "gene14"
[15] "gene15"
> paste0("gene",1:15)#循环补齐原理
 [1] "gene1"  "gene2"  "gene3"  "gene4"  "gene5"  "gene6"  "gene7" 
 [8] "gene8"  "gene9"  "gene10" "gene11" "gene12" "gene13" "gene14"
[15] "gene15"
> x <- 8:12
> #根据逻辑值取子集
> x[x==10]
[1] 10
> x[x<12]
[1]  8  9 10 11
> x[x %in% c(9,13)]
[1] 9
> #根据位置取子集
> x[4]
[1] 11
> x[2:4]
[1]  9 10 11
> x[c(1,5)]
[1]  8 12
> x[-4]
[1]  8  9 10 12
> x[-(2:4)]
[1]  8 12

2.5.修改向量中的某个/某些元素:取子集+赋值

> x[4] <- 40
> x
[1]  8  9 10 40 12
> x[x>3] <- 3
> x
[1] 3 3 3 3 3

练习3

1.将基因名"ACTR3B","ANLN","BAG1","BCL2","BIRC5","RAB","ABCT","ANF","BAD","BCF","BARC7","BALV"组成一个向量,赋值给x

> x = c("ACTR3B","ANLN","BAG1","BCL2","BIRC5","RAB","ABCT","ANF","BAD","BCF","BARC7","BALV")

2.用函数计算向量长度

> length(x)
[1] 12

3.用向量取子集的方法,选出第1,3,5,7,9,11个基因名。

> seq(1,11,2)
[1]  1  3  5  7  9 11
> x[seq(1,11,2)]
[1] "ACTR3B" "BAG1"   "BIRC5"  "ABCT"   "BAD"    "BARC7" 
> x[c(1,3,5,7,9)]
[1] "ACTR3B" "BAG1"   "BIRC5"  "ABCT"   "BAD"   

4.用向量取子集的方法,选出除倒数第2个以外所有的基因名。

> x[-11]
 [1] "ACTR3B" "ANLN"   "BAG1"   "BCL2"   "BIRC5"  "RAB"    "ABCT"  
 [8] "ANF"    "BAD"    "BCF"    "BALV"  
> length(x)-1#倒数第二个
[1] 11
> x[-(length(x)-1)]#反向取值
 [1] "ACTR3B" "ANLN"   "BAG1"   "BCL2"   "BIRC5"  "RAB"    "ABCT"  
 [8] "ANF"    "BAD"    "BCF"    "BALV"  

5.用向量取子集的方法,选出出在c("ANLN", "BCL2","TP53")中有的基因名。

> # 提示:%in%
> y = c("ANLN", "BCL2","TP53")
> x %in% y
 [1] FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[12] FALSE
> x[x %in% y]
[1] "ANLN" "BCL2"

6.修改第6个基因名为"a"并查看是否成功

> x[6] <- "a"

7.生成100个随机数: rnorm(n=100,mean=0,sd=18)

> z = rnorm(n=100,mean=0,sd=18)
> #将小于-2的统一改为-2,将大于2的统一改为2
> z[z< (-2)] <- -2#加括号shift+9或者空格  不然成了赋值号
> z[z>2] <- 2
> 

1.两个不同类型的向量合并在一起会怎么样?
1.两个不同类型的向量合并在一起会怎么样?
2.如何将两个向量合到一起,组成一个长向量?
1.两个不同类型的向量合并在一起会怎么样?
2.如何将两个向量合到一起,组成一个长向量?
3.如何在向量首/尾/中间某位置增加一个元素?

你可能感兴趣的:(Day1-R 01-get_start)