How to split train/test data 50% by class and grouping by Object ID in R?
Instead of:

dataTrain &lt;- EXTRACTED[trainIndeks,]
dataTest &lt;- EXTRACTED[-trainIndeks,]


try:

dataTrain &lt;- EXTRACTED[ID %% 2 == 1,]
dataTest  &lt;- EXTRACTED[ID %% 2 == 0,]


dataTrain will contain only odd ID, dataTest only even ones.

Using this method you may lose the balance of CLASS distribution between dataTrain and dataTest, but you may improve it by replication or removing of a few records, or by checking the distribution in other splitting, e.g.

dataTrain &lt;- EXTRACTED[ID %% 4 &lt; 2,]
dataTest  &lt;- EXTRACTED[ID %% 4 &gt; 1,]


EDIT:

For random changing the split, you can use:

set.seed(123)
N &lt;- 10
#N &lt;- round(max(EXTRACTED$ID)/10) # for more random grouping
p &lt;- 0.5 # train/(train+test) ratio
idx &lt;- sample(0:(N-1),round(N*p))
dataTrain &lt;- EXTRACTED[ID %% N %in% idx,]
dataTest  &lt;- EXTRACTED[!(ID %% N %in% idx),]