zl程序教程

您现在的位置是:首页 >  大数据

当前栏目

随机抽样以及数据探索

数据 探索 以及
2023-06-13 09:13:59 时间

一、随机抽样

在做统计分析的过程中,经常需要进行随机抽样,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

写在最后:有时间我们会努力更新的。大家互动交流可以前去论坛,地址在下面,复制去浏览器即可访问,弥补下公众号没有留言功能的缺憾。原地址暂未启用(bioinfoer.com)。

sx.voiceclouds.cn

有些板块也可以预设为大家日常趣事的分享等,欢迎大家来提建议。