2. Introduction to R 2
1. Simple Random Sampling
• Sampling from a vector or range of numbers
• Sampling with replacement
sample (c(1:10), size=3, replace=T)
• Sampling without replacement
sample (c(1:10), size=3, replace =F)
• “sampling” package can used for sampling in RStudio.
install.packages(“sampling”)
3. Introduction to R 3
• Randomly selecting rows from a dataset
Method 1 : Here the sample function in r is used to
select a random subset of 5 rows from a
large data frame.
Syntax – Dataset_name [row_no,coloumn_no]
>Data1[sample(nrow(Data1), 5), ]
>Data1[sample(nrow(Data1), 5),c(1,3,5) ]
4. Introduction to R 4
Method 2 : Using dplyr package
>install.packages(“dplyr”)
>library(dplyr)
Syntax –
Dataset_name[row_no,coloumn_no]
>sample_n(Data1, 10)
(Can be used “replace” function at
anytime)
5. Introduction to R 5
2. Stratified Sampling
Syntax :
> strata(data, stratanames, size, method=, description)
data - data frame
Stratanames - stratification variable
size - vector of stratum sample sizes
method - method to select units
(simple random sampling without replacement (srswor)
or simple random sampling with replacement (srswr))
description - a message is printed if its value is TRUE
(the message gives the number of selected units and
the number of the units in the population)
6. Introduction to R 6
Example
>Stratified <- strata(Data1,
stratanames = c("Sex"),
size = c(50,50),method = 'srswor',
description = TRUE)
> Stratified
To obtain the sample data frame
>Stratified_sample<getdata(Data1,
Stratified)
>Stratified_sample
7. Introduction to R 7
3. Cluster Sampling
Syntax :
> cluster(data, clustername, size, method, description)
data - data frame
clustername - clustering variable
size - sample size
method - method to select clusters
(simple random sampling without replacement (srswor)
or simple random sampling with replacement (srswr))
description - a message is printed if its value is TRUE
the message gives the number of selected units and the
number of the units in the population
8. Introduction to R 8
Example
>Clustered1<-cluster(Data1,
clustername = c("Sex"),size = 1,
method ='srswor',description = TRUE)
> Clustered1
To obtain the sample data frame
>Clustered1_sample1<-getdata(Data1,
Clustered1)
> Clustered1_sample1
9. Introduction to R 9
Systematic Sampling
Functions in R
A function is a set of statements organized together to
perform a specific task.
Execute the function with necessary parameters.
Syntax :
function_name=function(par1,par2,....)
{
..........
}
10. Introduction to R 10
Eg:
To find mean of a sequence with a given starting value
>Seq(from, to, by)
from – starting value
to – ending value
by – increment of the sequence
11. Introduction to R 11
> sys.sample<-function(N,n){
k=floor(N/n)
r=sample(1:k,1)
sys.sample=seq(r,r+k*(n-1),k)
print(sys.sample)
}
Syntax: sys.sample(N,n)
> sys.sample(4177,100)
To obtain the sample dataframe
>systematic_sample<-Data1[sys.sample
(4177,100),]
> systematic_sample