R语言 随机抽样

本文介绍了R语言中进行简单随机抽样的三种方法:使用sampling包中的srswr函数进行有放回抽样,使用srswor函数进行不放回抽样,以及使用内建的sample函数实现放回或不放回抽样。通过示例详细展示了如何使用这些函数抽取样本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 简单随机抽样是指从总体N个单位中任意抽取n个单位作为样本,使每个可能的样本被抽中的概率相等的一种抽样方式
  • 可通过srswr函数,srsworsample函数实现
  • srswr函数和srswor函数在sampling包中,使用前需要先加载sampling包
    srswr(n, N)
    srswor(n, N)
    sample(x, size, replace = FALSE, prob = NULL)

    示例:
install.packages("sampling")
#  运行结果:
#  WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:
#  
#  https://cran.rstudio.com/bin/windows/Rtools/
#  Installing package into ‘C:/Users/Admin/Documents/R/win-library/3.6’
#  (as ‘lib’ is unspecified)
#  also installing the dependency ‘lpSolve’
#  
#  试开URL’https://cran.rstudio.com/bin/windows/contrib/3.6/lpSolve_5.6.15.zip'
#  Content type 'application/zip' length 748088 bytes (730 KB)
#  downloaded 730 KB
#  
#  试开URL’https://cran.rstudio.com/bin/windows/contrib/3.6/sampling_2.8.zip'
#  Content type 'application/zip' length 905090 bytes (883 KB)
#  downloaded 883 KB
#  
#  package ‘lpSolve’ successfully unpacked and MD5 sums checked
#  package ‘sampling’ successfully unpacked and MD5 sums checked
#  
#  The downloaded b
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值