磨刀不误砍柴工:小果给你讲述r语言中随机数生成和变量创建的实用函数
公众号后台回复“111”
领取本篇代码、基因集或示例数据等文件
文件编号:240430
需要租赁服务器的小伙伴可以扫码添加小果,此外小果还提供生信分析,思路设计,文献复现等,有需要的小伙伴欢迎来撩~
getwd()

setwd('C:/Users/10395/Desktop/')
getwd()

#rep(x,times,each,length.out) 的使用
rep(1:4)
rep(1:4,times = 3) #按元素顺序重复三次
rep(1:4,each = 3) #每个元素依次重复三次
rep(1:4,each = 4,length.out = 3) #用length.out限制输出数据的长度

#paste(x1,x2,x3,,, sep) 的使用,用来链接两个元素
paste(letters[1:3],1:3)
paste(letters[1:3],1:3,sep = '')

#paste0(x1,x2,x3) 的使用,与paste相比可以不用使用sep进行间隔,
paste0(letters[1:3],1:3) # 其实是相当于paste(letters[1:3],1:3,sep = '')

#seq(from = , to = , by = ,length.out = NULL) 生成序列
seq(2,10) #生成从2到10的数列
seq(2,10,by = 4) #在2到10的数列中每隔4个数取一个

#对rep和paste0组合使用
rep(paste0(letters[1:3],LETTERS[1:3],1:3),times = 3)

#矩阵的创建rbind,cbind
#rbind的使用
rbind(1:5)
xiaoguo <- c(10,5)
rbind(1:4, c = 2, "a++" = 10, xiaoguo, deparse.level = 0)
rbind(1:4, c = 2, "a++" = 10, xiaoguo)
#cbind的使用
cbind(0:5)
cbind(0:5,1:3)
#结合使用
cbind(0:3,1:3,rbind(1:4, c = 2, "a++" = 10,xiaoguo))

aa
#colnames查看矩阵的列名
colnames(aa) #相当于names的作用
colnames(aa) <- c(paste0(letters[1:6],1:6))
colnames(aa)
#row.names查看列名
row.names(aa)
row.names(aa) <- c(paste0(LETTERS[1:4],1:4))
row.names(aa)

#sample的使用sample(x,size,replace)
sample(1:5) #对1:5进行排序
sample(1:5,4) #在1:5中随机选取4个
sample(1:5,6,replace = T) #有重复的选出来6个数
#runif(n,min,max)的使用
runif(10,3,9) #均匀分布
#rnorm(x,mean,sd)的使用
rnorm(10,mean = 4, sd = 2) #正态分布
#round(x,digits)的使用
round(rnorm(10,3,6),2) #对数据进行四舍五入

bb <- data.frame(A1 = rnorm(15,1,1),
A2 = rnorm(15,2,1),
A3 = runif(15,3,9),
A4 = rnorm(15,4,1),
A5 = rnorm(15,5,1))
head(bb)

#加载包
library(ggridges)
library(reshape2)
library(ggplot2)
#数据转换
bb <- melt(bb)
head(bb)

P1 <- ggplot(bb, aes(x = value, y = variable)) +
geom_density_ridges(aes(fill = variable))
P1

小果还提供思路设计、定制生信分析、文献思路复现;有需要的小伙伴欢迎直接扫码咨询小果,竭诚为您的科研助力!

定制生信分析
服务器租赁
扫码咨询小果


往期回顾
01 |
02 |
03 |
04 |