Using Discretization from Training Set on Test Set in R I am currently discretizing my training set in R with discretize from the bnlearn package.

library(bnlearn) 
discretize(train, method = "quantile", breaks = 2)


The lower bound of one interval and the upper bound of the other interval are the minimum and the maximum of the respective column.

Executing the same command on the test set

discretize(test, method = "quantile", breaks = 2)


will result in a different discretization, as the minimum and the maximum will likely be different on the test set. Is it possible in R (with another library or command) to transfer a discretization from a training set to a test set?

Apparantly this is easy to do in Weka and Orange, however, I would prefer to do this in R (not using RWeka).
You need to save the breaks/endpoints when you perform the bucketing/discretization on the training set as a (named) vector. The same breaks/endpoints can be then re-used on test set.

The I am giving below sample code using cut function in base R to keep the answer more generally applicable (rather than giving answer specific to bnlearn package). Note the way output of quantile function is saved in vector breaks_to_use in line 3 below and re-used while applying cut on test data. You can (hopefully) do the same using the breaks argument in discretize function.

training_data &lt;- runif(100)
test_data &lt;- runif(100)
breaks_to_use &lt;- quantile(training_data, seq(0, 1, 0.25))
discretized_training_data &lt;- cut(training_data, breaks = breaks_to_use)
discretized_test_data &lt;- cut(test_data, breaks = breaks_to_use)