0% found this document useful (0 votes)
3 views3 pages

Sampling Using R

The document outlines various sampling methods including simple random sampling, stratified sampling, systematic sampling, and cluster sampling using R programming. It provides code snippets for each method, demonstrating how to read data, calculate population and sample means, and generate samples from a dataset. Each sampling technique is illustrated with examples from different datasets, highlighting the process and results.

Uploaded by

singhjai2818
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Sampling Using R

The document outlines various sampling methods including simple random sampling, stratified sampling, systematic sampling, and cluster sampling using R programming. It provides code snippets for each method, demonstrating how to read data, calculate population and sample means, and generate samples from a dataset. Each sampling technique is illustrated with examples from different datasets, highlighting the process and results.

Uploaded by

singhjai2818
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

path<-"C:/Users/akash/Downloads/scoringData.

csv"

set.seed(7)

df<-read.csv(path)

N<-nrow(df)

#n<-readline(prompt="Enter the Sample Size: ")

#n<-as.numeric(n)

n<-0.70*N

split<-sample(1:N, size = n)

simple<-df[split,]

#print("Simple Random Sample extracted is as follows: ")

#print(simple)

pmn<-mean(df$energy)

smn<-mean(simple$energy)

cat("\nPopulation Mean value is ",pmn)

cat("\nSample Mean value is ",smn)


STRATAFIED SAMPLING-

library("sampling")

df<-mtcars

set.seed(6)

#View(df)

vec<-df$am

N<-length(vec)

t<-table(vec)

n<-readline(prompt="Enter the sample size: ")

n<-as.numeric(n)

u<-unique(vec)

sz<-length(u)

prop<-c()

for(i in 1:sz)

x<-t[i]

p<-(x*n)/N

p<-round(p,0)

prop<-c(prop,p)

stratas<-strata(df, c("am"),size=prop, method="srswor")

stratified_sample<-getdata(df,stratas)

pmn<-mean(df$mpg)

smn<-mean(stratified_sample$mpg)

cat("\nMean Mileage of Population is ",pmn)

cat("\nMean Mileage of sample is ",smn)


SYSTEMATIC SAMPLING-

path<-"C:/Users/akash/Downloads/scoringData.csv"

df<-read.csv(path)

set.seed(7)

df<-df[order(df$loudness,decreasing = FALSE , na.last = FALSE),]

vec<-df$loudness

N<-length(vec)

n<-readline(prompt = "Enter the sample size: ")

n<-as.numeric(n)

k<-round(N/n,0)

f<-sample(1:k,1)

s<-seq(f,N,k)

sys_sample<-df[s,]

pmn<-mean(df$loudness)

smn<-mean(sys_sample$loudness)

cat("\nPolpulation mean value is: ",pmn)

cat("\nSample mean value is: ",smn)

CLUSTER SAMPLING –

path<-"C:/Users/akash/Downloads/cases.csv"

df<-read.csv(path)

set.seed(8)

u<-unique(df$country)

clusters<-sample(u, size = 40)

cluster_sample<-df[df$country %in% clusters,]

print("One stage cluster sample is as follows:- ")

View(cluster_sample)

t<-table(cluster_sample$country)

print("Frequency Distribution of Countries in the cluster is as follows:-")

print(t)

You might also like