Last active
January 3, 2016 16:39
-
-
Save RanaivosonHerimanitra/8491081 to your computer and use it in GitHub Desktop.
After the determination of the sample size of each Strata, this function built with 'data.table' package samples units.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require(data.table) | |
mysample=alloc_opti_data.table() | |
get_echantillon_data.table=function( | |
dataset=yourDataset, | |
alea=1435, | |
set.alea=TRUE, | |
strate="strate2013", | |
eff=mysample[,list(strate2013,nh=round(nh))]) | |
{ | |
if (set.alea==TRUE) { | |
set.seed(alea) | |
} | |
#convert to datatable for speed: | |
dataset=data.table(dataset) | |
setkey(dataset,strate) | |
#to dataframe: | |
eff=data.frame(eff) | |
#echantillon=sample(which(ese_op2013_reste[,strate]==eff[1,2]),eff[1,1],replace=F) | |
idx=sample( row.names(dataset[J(eff[1,strate])]),eff[1,"nh"],replace=F) | |
for ( j in 2:nrow(eff) ) { | |
set.seed(alea+j) | |
idx=c(idx, | |
sample( row.names(dataset[J(eff[j,strate])]),eff[j,"nh"],replace=F) | |
) | |
} | |
write.table(echantillon,"echantillon.csv",sep=";") | |
dataset=data.frame(dataset) | |
return ( dataset[as.numeric(idx),] ) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment