生信课程note-2

2023-02-06 20:48:05 浏览数 (1)

title: "noteH"

output: html_document

date: "2023-02-06"

class 2

2.1数据类型转换的优先顺序:

数值型会转变成字符型

c(1,"a") "1" "a"

逻辑型会转变成字符型

逻辑型会变成数值型

2.2 对单个向量进行的操作

(1)赋值

x <- c(1,3,5,1) #规范的赋值符号 Alt 减号 是等号的意思

x回车可以看x里面是什么东西

x = c(1,3,5,1) #随意的写法

#赋值 输出一起实现

x <- c(1,3,5,1);x

(x <- c(1,3,5,1))

注:

建议/不建议使用的变量名:

yes:x,y,z,A,B no: c "x"

yes:jimmy,bala no:seq,limma

yes:x1,bala1 no:1x,1bala

yes:x_1 no: x 1

yes:x.1 no: x-1 , x 1

yes:gp1_number no: 芭芭拉

(2)简单数学计算

x 1

log(x)

sqrt(x)

(3)根据某条件进行判断,生成逻辑型向量

x>3

x==3

(4)初级统计

max(x) #最大值

min(x) #最小值

mean(x) #均值

median(x) #中位数

var(x) #方差

sd(x) #标准差

sum(x) #总和

length(x) #长度

unique(x) #去重复 从左到右第二次出现才叫重复。

duplicated(x) #对应元素是否重复 重复是T

table(x) #重复值统计

sort(x) 默认排序是从小到大

sort(x,decreasing = F) 其实是升序

sort(x,decreasing = T) 是降序

2.3对两个向量的操作

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

x == y

y == x

(2)数学计算

x y

(3)连接

paste(x,y,sep=",")

paste与paste0的区别

paste(x,y)

paste0(x,y)

paste(x,y,sep = "")

paste(x,y,sep = ",")

#当两个向量长度不一致

x = c(1,3,5,6,2)

y = c(3,2,5)

x == y #判断是否相等

会发生循环补齐--看ppt

会发生循环补齐的是x==y;paste(x,y);x y

#利用循环补齐简化代码

paste0(rep("x",3),1:3)

paste0("x",1:3)

(4)交集、并集、差集

intersect(x,y) 系统会去重复

union(x,y)

setdiff(x,y) x里有y里没有

setdiff(y,x) y有x没有

重点: %in%

x %in% y #x的每个元素在y中存在吗

y %in% x #y的每个元素在x中存在吗

2.4.向量筛选(取子集)--

x <- 8:12

#根据逻辑值取子集

xx==10

xx<12

xx %in% c(9,13)

#根据位置取子集

x4

x2:4

xc(1,5) 取出第一个和第五个

x-4 去掉第四个

x-(2:4)

2.5.修改向量中的某个/某些元素:

取子集 赋值

#改一个元素

x4 <- 40

x

#改多个元素

xc(1,5) <- c(80,20)

x

2.6 简单向量作图

k1 = rnorm(12);k1

k2 = rep(c("a","b","c","d"),each = 3);k2

plot(k1)

boxplot(k1~k2) #

实操:

R Markdown

代码语言:text复制
# 1.生成1到15之间所有偶数
seq(from = 1,to = 15,by = 2)`
## [1]  1  3  5  7  9 11 13 15
seq(from = 2,to = 15,by = 2)
## [1]  2  4  6  8 10 12 14
# 2.生成向量,内容为:"student2"  "student4"  "student6"  "student8"  "student10" "student12"
# "student14" 
# 提示:paste0
paste0(rep("student",times = 7),seq(from = 2, to = 15,by = 2))
## [1] "student2"  "student4"  "student6"  "student8"  "student10" "student12" "student14"
# 3.将两种不同类型的数据用c()组合在一起,看输出结果
c(1,"a")
代码语言:txt复制
## [1] "1" "a"
代码语言:text复制
c(TRUE,"a")
代码语言:txt复制
## [1] "TRUE" "a"
代码语言:text复制
c(1,TRUE)
代码语言:txt复制
## [1] 1 1
代码语言:text复制
# 4.用函数计算向量g的长度
load("gands.Rdata")
length(g)
代码语言:txt复制
## [1] 100
代码语言:text复制
# 5.筛选出向量g中下标为偶数的基因名。
seq(2,100,2)
代码语言:txt复制
##  [1]   2   4   6   8  10  12  14  16  18  20  22  24  26  28  30  32  34  36  38  40  42
## [22]  44  46  48  50  52  54  56  58  60  62  64  66  68  70  72  74  76  78  80  82  84
## [43]  86  88  90  92  94  96  98 100
代码语言:text复制
g[seq(2,100,2)]
代码语言:txt复制
##  [1] "CRAMP1L"    "PRSS8"      "CRAMP1L"    "SLCO1C1"    "COMMD1"     "CCT4"      
##  [7] "RAB7A"      "ZDHHC16"    "MYL12B"     "SNRPE"      "ZNF586"     "GGT7"      
## [13] "RAB7A"      "AFG3L2"     "AC104581.1" "MPP2"       "ATP2A2"     "SNRPE"     
## [19] "PRSS8"      "ZNF461"     "CECR5"      "CLEC17A"    "ATG10"      "ATG10"     
## [25] "SLC25A25"   "KRTAP4-3"   "SLCO1C1"    "GGT7"       "GSTP1"      "UBAC1"     
## [31] "NYNRIN"     "MYL12B"     "KCND1"      "RGPD3"      "C10orf128"  "SLC30A9"   
## [37] "GGT7"       "TUBA4A"     "KLHDC8A"    "HBP1"       "MARC2"      "LCP1"      
## [43] "OR2D3"      "LIPE"       "LIPE"       "CANX"       "ATP6V1B2"   "MARC2"     
## [49] "LCP1"       "HOOK2"
代码语言:text复制
# 6.向量g中有多少个元素在向量s中存在(要求用函数计算出具体个数)?将这些元素筛选出来
# 提示:%in%
table(g %in% s)
代码语言:txt复制
## 
## FALSE  TRUE 
##    37    63
代码语言:text复制
g[g %in% s]
代码语言:txt复制
##  [1] "GFM2"          "SLCO1C1"       "NYNRIN"        "COMMD1"        "COMMD1"       
##  [6] "AC017081.1"    "RAB7A"         "CASKIN2"       "GGT7"          "SNRPE"        
## [11] "RGPD3"         "ZNF586"        "COMMD1"        "GGT7"          "URB1"         
## [16] "RAB7A"         "MPP2"          "AFG3L2"        "URB1"          "AC104581.1"   
## [21] "MPP2"          "SNRPE"         "ARHGAP1"       "ZNF461"        "OR2D3"        
## [26] "CECR5"         "SPDL1"         "CLEC17A"       "ZNF461"        "ATG10"        
## [31] "ATG10"         "ATG10"         "SLC25A25"      "SLC30A9"       "SLCO1C1"      
## [36] "GGT7"          "CASKIN2"       "GSTP1"         "MPP2"          "NYNRIN"       
## [41] "INTS12"        "MPP2"          "RGPD3"         "RGPD3"         "SLC30A9"      
## [46] "C10orf128"     "HBD"           "SLC30A9"       "GGT7"          "HEPH"         
## [51] "RP5-1021I20.4" "KLHDC8A"       "HBD"           "ZNF586"        "CECR5"        
## [56] "OR2D3"         "LIPE"          "INTS12"        "LIPE"          "SPDL1"        
## [61] "SLCO1C1"       "GGT7"          "CECR5"

# 7.生成10个随机数: rnorm(n=10,mean=0,sd=18),用向量取子集的方法,取出其中小于-2的值
z = rnorm(n=10,mean=0,sd=18)
z[z<(-2)]

## [1]  -4.657298  -8.565170 -18.576409  -2.250447

引自生信技能树

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like t

0 人点赞