生信人

找回密码
立即注册
搜索
热搜: 活动 交友 discuz
发新帖

0

收听

12

听众

318

主题
发表于 2022-9-1 23:07:06 | 查看: 1247| 回复: 0
一、随机抽样
       在做统计分析的过程中,经常需要进行随机抽样,R 提供了多种生成随机数的函数,并且可以进行多种形式的抽样。
  1. x <- 1:10
  2. x
  3. sample(x,size = 5)
  4. sample(x,size = 5,replace = F)
  5. sample(x,size = 5,replace = T)
  6. set.seed(1234)
  7. sample(x,5,replace = F)#先设置上一步,这一步结果就固定了
  8. set.seed(1234)
  9. sample(x,4)
  10. set.seed(1234)
  11. sample(x,3)
  12. #按比例抽样
  13. x <- 1:100
  14. sample(x,size = 0.25*length(x))
复制代码

二、利用 R 语言斗地主
  1. rm(list = ls())
  2. type <- c("red","spades","cube","plum")
  3. amount <- c("A",2:10,"J","Q","K")
  4. a <- rep(type,each = 13)
  5. b <- rep(amount,4)
  6. paste(a,b)

  7. group <- expand.grid(type,amount)
  8. poker <- paste(group$Var2,group$Var1,sep = "-")
  9. poker
  10. poker[c(53,54)] <- c("black Joker","red Joker")
  11. set.seed(666)
  12. shuffle <- sample(poker,54,replace = F)
  13. head(shuffle) #运行种子数,每次牌一样
  14. dipai <- shuffle[52:54]
  15. shuffle <- shuffle[-c(52:54)]
  16. one <- shuffle[c(T,F,F)]
  17. two <- shuffle[c(F,T,F)]
  18. three <- shuffle[c(F,F,T)]
复制代码

三、探索数据
  1. rm(list = ls())
  2. x <- read.csv('WHO.csv',row.names = 1)
  3. x$CountryID <- rownames(x)
  4. colnames(x)[grep('Pop',colnames(x))]
  5. y <- data.frame(x$CountryID,x$Population_total)
  6. sort(y$Population_total, decreasing = T)
  7. order(y$Population_total, decreasing = T)
  8. y[order(y$x.Population_total, decreasing = T),][1:10,]
  9. sum(y$x.Population_total,na.rm = T)/100000000
复制代码

您需要登录后才可以回帖 登录 | 立即注册

QQ|Archiver|手机版|小黑屋|生信人 ( 萌ICP备20244422号 )

GMT+8, 2024-11-23 17:32 , Processed in 0.084749 second(s), 29 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表