# 1.生成1到15之间所有偶数
seq(from = 2,to = 15,by = 2)## [1] 2 4 6 8 10 12 14##参数可以省略
seq(2,15,2)## [1] 2 4 6 8 10 12 14# 2.生成向量,内容为:"student2" "student4" "student6" "student8" "student10" "student12" "student14"
paste0(rep("student",times = 7),seq(from = 2, to = 15,by = 2))## [1] "student2" "student4" "student6" "student8" "student10" "student12" "student14"paste0(rep("student",times = 3),seq(from = 2, to = 15,by = 2))## [1] "student2" "student4" "student6" "student8" "student10" "student12" "student14"paste0(rep("student",times = 7),seq(from = 2, to = 6,by = 2))## [1] "student2" "student4" "student6" "student2" "student4" "student6" "student2"若拼贴的两个向量元素数目不相等,则循环补齐(见下文)循环补齐是R语言中的重要原则
# 3.将两种不同类型的数据用c()组合在一起,看输出结果
c(1,"a")## [1] "1" "a"c(TRUE,"a")## [1] "TRUE" "a"c(1,TRUE)## [1] 1 1字符型>数值型>逻辑型
##今日内容
x <- c(1,3,5,1) #规范的赋值符号 快捷键Alt+减号
x## [1] 1 3 5 1#赋值+输出一起实现
x <- c(1,3,5,1);x## [1] 1 3 5 1(x <- c(1,3,5,1))## [1] 1 3 5 1分号;可将多行代码放一行
注意变量命名规范
x <- c(1,3,5,1)
x+1## [1] 2 4 6 2log(x)## [1] 0.000000 1.098612 1.609438 0.000000sqrt(x)## [1] 1.000000 1.732051 2.236068 1.000000x>3## [1] FALSE FALSE TRUE FALSEx==3## [1] FALSE TRUE FALSE FALSEmax(x) #最大值## [1] 5min(x) #最小值## [1] 1mean(x) #均值## [1] 2.5median(x) #中位数## [1] 2var(x) #方差## [1] 3.666667sd(x) #标准差## [1] 1.914854sum(x) #总和## [1] 10length(x) #长度 元素个数## [1] 4unique(x) #去重复 从左往右第二次及以上出现的元素为重复元素## [1] 1 3 5duplicated(x) #对应元素是否重复 返回逻辑值向量## [1] FALSE FALSE FALSE TRUE!duplicated(x) # 第一次出现是TRUE 重复是FALSE## [1] TRUE TRUE TRUE FALSEtable(x) #重复值统计## x
## 1 3 5
## 2 1 1sort(x) # 默认升序## [1] 1 1 3 5sort(x,decreasing = F)## [1] 1 1 3 5sort(x,decreasing = T)## [1] 5 3 1 1作业2优化 前面student个数是数出来的,但是在R语言中,能用函数代替就不要自己数,除非这代码只用一次
a <- seq(2,15,2)
paste0(rep("student",times = length(a)),a)## [1] "student2" "student4" "student6" "student8" "student10" "student12" "student14"x = c(1,3,5,1)
y = c(3,2,5,6)#(1)比较运算,生成等长的逻辑向量
x == y ## [1] FALSE FALSE TRUE FALSEy == x## [1] FALSE FALSE TRUE FALSE#(2)数学计算
x + y## [1] 4 5 10 7#(3)连接2.连接paste0和paste
paste(x,y,sep=",")## [1] "1,3" "3,2" "5,5" "1,6"#paste与paste0的区别:都是将元素转化为字符串后拼接,paste可以通过sep定义连接符号,默认为空格,也可以无缝连接。paste0没有sep参数,默认无缝连接,可以看成特殊的paste
paste(x,y)## [1] "1 3" "3 2" "5 5" "1 6"paste0(x,y)## [1] "13" "32" "55" "16"paste(x,y,sep = "")## [1] "13" "32" "55" "16"paste(x,y,sep = ",")## [1] "1,3" "3,2" "5,5" "1,6"#当两个向量长度不一致
x = c(1,3,5,6,2)
y = c(3,2,5)
x + y## Warning in x + y: 长的对象长度不是短的对象长度的整倍数## [1] 4 5 10 9 4x == y # 啊!warning!## Warning in x == y: 长的对象长度不是短的对象长度的整倍数## [1] FALSE FALSE TRUE FALSE TRUE因此作业2可以继续优化
paste0("student",seq(2,15,2))## [1] "student2" "student4" "student6" "student8" "student10" "student12" "student14"#(4)交集、并集、差集 注意会去重
intersect(x,y)# 交集## [1] 3 5 2union(x,y) # 并集## [1] 1 3 5 6 2setdiff(x,y) # x里的哪些元素y里没有## [1] 1 6setdiff(y,x) # y里的哪些元素x里没有## numeric(0)# %in% 返回逻辑值向量,与前面向量个数一致,不会去重,注意与intersect的区别
# 与==的区别是元素是否一一对应比较
x %in% y #x的每个元素在y中存在吗## [1] FALSE TRUE TRUE FALSE TRUEy %in% x #y的每个元素在x中存在吗## [1] TRUE TRUE TRUEx <- 8:12
#根据逻辑值取子集
x[x == 10]## [1] 10x[x < 12]## [1] 8 9 10 11x[x %in% c(9,13)]## [1] 9#根据位置取子集
x[4]## [1] 11x[2:4]## [1] 9 10 11x[c(1,5)]## [1] 8 12x[-4] # 这里负号是排除,不是倒数的意思!!!与python是不一样的!!!## [1] 8 9 10 12x[-(2:4)]## [1] 8 12# 也可以给元素命名后根据名字取子集,后面会讲x## [1] 8 9 10 11 12#改一个元素
x[4] <- 40
x## [1] 8 9 10 40 12#改多个元素
x[c(1,5)] <- c(80,20)
x## [1] 80 9 10 40 20# 注意一定要赋值,不然白改k1 = rnorm(12);k1## [1] -0.5911213 0.1683967 0.5130066 -0.8738898 -0.8950050 0.6982776 0.7872991 0.2893432
## [9] 1.5340035 -0.4590547 0.1422255 0.5780768k2 = rep(c("a","b","c","d"),each = 3);k2## [1] "a" "a" "a" "b" "b" "b" "c" "c" "c" "d" "d" "d"## rep加不加each重复模式不同哦
plot(k1)
boxplot(k1~k2) #boxplot画箱线图
tmp = sort(log_rank_p[log_rank_p<0.05])tmp = sort(log_rank_p)[log_rank_p<0.05]上面是对的。表面上看是括号位置的问题,实际上是运算顺序和筛选条件是否匹配被筛选向量的问题。下面代码中,中括号内得到的逻辑值向量是根据log_rank_p的元素顺序来的,但是它作为筛选条件筛选的是sort后的log_rank_p,顺序已经变了,所以得到的结果是错的。
引用自生信技能树
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。