如何在R中使用sample()获取样本?

时间:2020-02-23 14:43:54  来源:igfitidea点击:

让我们了解R中最常用的函数之一sample()。
在数据分析中,对数据进行采样是分析师最常用的过程。
要研究和理解数据,有时取样是最好的方法,并且在大数据的情况下通常是正确的。

R提供了标准函数sample()来从数据集中获取示例。
许多业务和数据分析问题将需要从数据中取样。
随机数据是在此过程中生成的,有无替换,如下节所示。

R中sample()的语法

sample(x, size, replace = FALSE, prob = NULL)

  • x –向量或者数据集。

  • 大小–示例大小。

  • 替换–替换或者不替换值。

  • 替换–替换或者不替换值。

  • 概率–概率权重

更换示例

您可能想知道,正在取样替换的示例 是什么?

好吧,当您从列表或者数据中取样时,如果您指定replace = TRUE或者T,则该函数将允许重复值。

请遵循以下示例,该示例清楚地说明了这种情况。

#sample range lies between 1 to 5
x<- sample(1:5)
#prints the samples
x
Output -> 3 2 1 5 4

#samples range is 1 to 5 and number of samples is 3
x<- sample(1:5, 3)
#prints the samples (3 samples)
x
Output -> 2 4 5

#sample range is 1 to 5 and the number of samples is 6
x<- sample(1:5, 6)
x
#shows error as the range should include only 5 numbers (1:5)
Error in sample.int(length(x), size, replace, prob) : 
cannot take a sample larger than the population when 'replace = FALSE'

#specifing replace=TRUE or T will allow repetition of values so that the function will generate 6 samples in the range 1 to 5. Here 2 is repeated.
 
x<- sample(1:5, 6, replace=T)
Output -> 2 4 2 2 4 3

R中未更换的示例

在这种情况下,我们将取样而不更换示例 。
整个概念如下所示。

在不替换的情况下,将使用函数replace = F,它将不允许重复值。

#samples without replacement 
x<-sample(1:8, 7, replace=F)
x
Output -> 4 1 6 5 3 2 7
x<-sample(1:8, 9, replace=F)
Error in sample.int(length(x), size, replace, prob) :
cannot take a sample larger than the population when 'replace = FALSE'

#here the size of the sample is equal to range 'x'. 
x<- sample(1:5, 5, replace=F)
x
Output -> 5 4 1 3 2

使用函数set.seed()进行采样

正如您可能会遇到的那样,当您采样时,它们将是随机的,并且每次都会改变。
为了避免这种情况,或者每次都不想使用不同的示例,可以使用set.seed()函数。

set.seed()– set.seed函数在运行时会产生相同的序列。

这种情况如下所示,执行以下代码以每次获得相同的随机示例。

#set the index 
set.seed(5)
#takes the random samples with replacement
sample(1:5, 4, replace=T)
2 3 1 3

set.seed(5)
sample(1:5, 4, replace=T)
2 3 1 3

set.seed(5)
sample(1:5, 4, replace=T)
2 3 1 3

从数据集中获取示例

在本节中,我们将从Rstudio中的数据集中生成示例。

此代码将从" ToothGrowth"数据集中获取10行作为示例并显示它。
这样,您可以从数据集中获取所需大小的示例。

#reads the dataset 'Toothgrwoth' and take the 10 rows as sample
df<- sample(1:nrow(ToothGrowth), 10)
df
--> 53 12 16 26 37 27  9 22 28 10
#sample 10 rows
ToothGrowth[df,]

  len supp dose
53 22.4   OJ  2.0
12 16.5   VC  1.0
16 17.3   VC  1.0
26 32.5   VC  2.0
37  8.2   OJ  0.5
27 26.7   VC  2.0
9   5.2   VC  0.5
22 18.5   VC  2.0
28 21.5   VC  2.0
10  7.0   VC  0.5

使用set.seed()函数从数据集中获取示例

在本节中,我们将使用set.seed()函数从数据集中获取示例。

执行以下代码,使用set.seed()从数据集中生成示例。

#set.seed function
set.seed(10)
#taking sample of 10 rows from the iris dataset. 
x<- sample(1:nrow(iris), 10)
x
--> 137  74 112  72  88  15 143 149  24  13
#displays the 10 rows
iris[x, ]
  Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
137          6.3         3.4          5.6         2.4  virginica
74           6.1         2.8          4.7         1.2 versicolor
112          6.4         2.7          5.3         1.9  virginica
72           6.1         2.8          4.0         1.3 versicolor
88           6.3         2.3          4.4         1.3 versicolor
15           5.8         4.0          1.2         0.2     setosa
143          5.8         2.7          5.1         1.9  virginica
149          6.2         3.4          5.4         2.3  virginica
24           5.1         3.3          1.7         0.5     setosa
13           4.8         3.0          1.4         0.1     setosa

多次执行代码时,将获得相同的行。
因为我们已经使用了set.seed()函数,所以这些值不会改变。

在R中使用sample()生成随机示例

好吧,我们将在问题的帮助下理解这个概念。

问题:一家礼品店已决定向其一位顾客提供惊喜礼物。
为此,他们收集了一些名称。
事情是从列表中选择一个随机名称。

提示:使用sample()函数生成随机示例。

如下所示,每次运行此代码时,它都会随机生成参与者名称示例。

#creates a list of names and generates one sample from this list
sample(c('Hyman','Rossie','Kyle','Edwards','Joseph','Paloma','Kelly','Alok','Jolie'),1)
--> "Rossie"
 sample(c('Hyman','Rossie','Kyle','Edwards','Joseph','Paloma','Kelly','Alok','Jolie'),1)
--> "Jolie"

sample(c('Hyman','Rossie','Kyle','Edwards','Joseph','Paloma','Kelly','Alok','Jolie'),1)
--> "Hyman"

sample(c('Hyman','Rossie','Kyle','Edwards','Joseph','Paloma','Kelly','Alok','Jolie'),1)
--> "Edwards"

sample(c('Hyman','Rossie','Kyle','Edwards','Joseph','Paloma','Kelly','Alok','Jolie'),1)
--> "Kyle"

通过设置概率采样

借助以上示例和概念,您已经了解了如何生成随机示例并从数据集中提取特定数据。

如果我说R允许您设置概率,则有些人可能会感到轻松,因为它可以解决许多问题。
让我们借助一个简单的示例来看看它是如何工作的。

我们以一家能够生产10枚手表的为例。
在这10块手表中,有20%被发现有缺陷。
让我们借助以下代码进行说明。

#creates a probability of 80% good watches an 20% effective watches.
 sample (c('Good','Defective'), size=10, replace=T, prob=c(.80,.20))
 
"Good"      "Good"      "Good"      "Defective" "Good"      "Good"     
"Good"      "Good"      "Defective" "Good"  

您还可以尝试进行如下所示的不同概率调整。

sample (c('Good','Defective'), size=10, replace=T, prob=c(.60,.40))
 
--> "Good"      "Defective" "Good"      "Defective" "Defective" "Good"     
 "Good"      "Good"      "Defective" "Good"