########## EXAMPLE 1 ##########
### unidimensional binning
# generate data
x <- seq(0, 1, length.out = 101)
# bin sample (default)
set.seed(1)
bin.sample(x)
# bin sample (return indices)
set.seed(1)
xs <- bin.sample(x, index.return = TRUE)
xs$x # sampled data
x[xs$ix] # indexing sampled data
# bin sample (return indices and breaks)
set.seed(1)
xs <- bin.sample(x, index.return = TRUE, breaks.return = TRUE)
xs$x # sampled data
x[xs$ix] # indexing sampled data
xs$bx # breaks
########## EXAMPLE 2 ##########
### bidimensional binning
# generate data
x <- expand.grid(x1 = seq(0, 1, length.out = 101),
x2 = seq(0, 1, length.out = 101))
# bin sample (default)
set.seed(1)
bin.sample(x)
# bin sample (return indices)
set.seed(1)
xs <- bin.sample(x, index.return = TRUE)
xs$x # sampled data
x[xs$ix,] # indexing sampled data
# bin sample (return indices and breaks)
set.seed(1)
xs <- bin.sample(x, index.return = TRUE, breaks.return = TRUE)
xs$x # sampled data
x[xs$ix,] # indexing sampled data
xs$bx # breaks
# plot breaks and 25 bins
plot(xs$bx, xlim = c(0, 1), ylim = c(0, 1),
xlab = "x1", ylab = "x2", main = "25 bidimensional bins")
grid()
text(xs$bx + 0.1, labels = 1:25)
Run the code above in your browser using DataLab