|
发表于 2022-9-1 23:07:06
|
查看: 1247 |
回复: 0
一、随机抽样
在做统计分析的过程中,经常需要进行随机抽样,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(53,54)] <- c("black Joker","red Joker")
- set.seed(666)
- shuffle <- sample(poker,54,replace = F)
- head(shuffle) #运行种子数,每次牌一样
- dipai <- shuffle[52:54]
- shuffle <- shuffle[-c(52:54)]
- one <- shuffle[c(T,F,F)]
- two <- shuffle[c(F,T,F)]
- three <- shuffle[c(F,F,T)]
复制代码
三、探索数据
- rm(list = ls())
- x <- read.csv('WHO.csv',row.names = 1)
- x$CountryID <- rownames(x)
- colnames(x)[grep('Pop',colnames(x))]
- y <- data.frame(x$CountryID,x$Population_total)
- sort(y$Population_total, decreasing = T)
- order(y$Population_total, decreasing = T)
- y[order(y$x.Population_total, decreasing = T),][1:10,]
- sum(y$x.Population_total,na.rm = T)/100000000
复制代码
|
|