Stratified Random Sampling Implementation how to in R? I have a dataset of 20 million rows. it is organized into strata (groups), and I need to sample from them. I need to create a smaller sampled dataset on which I bulid a regression model.
I need to first determine total sample size and then the sample size for each stratum, and then choose the training and test indexes. 
How does one implement all of this in R? can anyone provide some links to this?
 A: For a stratified sample you can use caTools library. If your factor variable is strata and you want 70% of the data as train, the code is 
library(caTools)
train_rows = sample.split(data$strata, SplitRatio=0.7)
train = data[ train_rows,]
test  = data[-train_rows,]

Ps: it should be:
library(caTools)
train_rows = sample.split(data$strata, SplitRatio=0.7)
train = data[ train_rows,]
test  = data[!train_rows,]

Now, it should be working
A: In addition to @lanenok's answer, you seem to need to determine the sample size per stratum. One way to do so is to use Neyman's allocation, which is implemented in the stratification package :
library(stratification)

## Example dataset
N <- 1000
id <- c(1:N)
y <- 10*runif(N) # Quantitative variable from which strata are constructed
df <- data.frame(cbind(id,y))

n <- 50 ## Example of sample size

testStrata <- strata.bh(y, c(2,4,7), n, Ls=4)

allocation <- testStrata$nh

In my example, 4 strata are constructed using the quantitative variable y, which range is (0,11) :


*

*Stratum 1 : $y \in [0,2]$

*Stratum 2 : $y \in [2,4]$

*Stratum 3 : $y \in [4,7]$

*Stratum 4 : $y \in [7,11]$


In my example, this gives you an allocation computed with Neyman's formula :
allocation = c(9,7,16,18)

Then, you can draw your sample using the package sampling :
library(sampling)

## These lines construct the strata variable for our example
df$strataId <- sapply(df$y, function(x) { 
  if (x<=2) {return(1)} 
  if(x>=2 && x<=4) {return(2)}
  if (x>=4 && x<=7) {return(3)}
   if(x>=7) {return(4)} 
  return(4)  
                    })

sample <- strata(df, c("strataId"), size=allocation)

which finally gives you 50 units, drawn using stratified sampling and the Neyman allocation.
A: I assume all the data which you have is in x
library(caret)
# considering response variable as strata
data_part <- createDataPartition(y = x$Response, 
                                 p = 0.7, list = F)
test <- x[-data_part,] # 30% data goes here
train <- x[data_part,] # 70% here

Hope it helps.
