生信喵 发表于 2022-9-1 23:07:06

随机抽样以及数据探索

一、随机抽样
       在做统计分析的过程中,经常需要进行随机抽样,R 提供了多种生成随机数的函数,并且可以进行多种形式的抽样。
x <- 1:10
x
sample(x,size = 5)
sample(x,size = 5,replace = F)
sample(x,size = 5,replace = T)
set.seed(1234)
sample(x,5,replace = F)#先设置上一步,这一步结果就固定了
set.seed(1234)
sample(x,4)
set.seed(1234)
sample(x,3)
#按比例抽样
x <- 1:100
sample(x,size = 0.25*length(x))
二、利用 R 语言斗地主
rm(list = ls())
type <- c("red","spades","cube","plum")
amount <- c("A",2:10,"J","Q","K")
a <- rep(type,each = 13)
b <- rep(amount,4)
paste(a,b)

group <- expand.grid(type,amount)
poker <- paste(group$Var2,group$Var1,sep = "-")
poker
poker <- c("black Joker","red Joker")
set.seed(666)
shuffle <- sample(poker,54,replace = F)
head(shuffle) #运行种子数,每次牌一样
dipai <- shuffle
shuffle <- shuffle[-c(52:54)]
one <- shuffle
two <- shuffle
three <- shuffle
三、探索数据
rm(list = ls())
x <- read.csv('WHO.csv',row.names = 1)
x$CountryID <- rownames(x)
colnames(x)
y <- data.frame(x$CountryID,x$Population_total)
sort(y$Population_total, decreasing = T)
order(y$Population_total, decreasing = T)
y
sum(y$x.Population_total,na.rm = T)/100000000
页: [1]
查看完整版本: 随机抽样以及数据探索