Tell me more ×
Cross Validated is a question and answer site for statisticians, data analysts, data miners and data visualization experts. It's 100% free, no registration required.

I am trying to classify a dataset which has 20 features and a class (target) feature. The problem is that "class-1" is found in 25% of cases while the rest 75% is "class-2" examples.

My question is : How can i under sample instances of class2 (or oversample class1 instances) in R?

I already tried to look for some answers but i would not like to use weighting.

Thank you

share|improve this question

1 Answer

up vote 2 down vote accepted

sample has a nice argument called prob. This is a vector of the same length as your x, with each element giving probability for being sampled. In my example, you would expect to get 5 in about 60% of cases.

> sample(1:5, 100, prob = c(0.1, 0.1, 0.1, 0.1, 0.6), replace = TRUE)
    [1] 5 5 5 5 4 5 5 2 2 2 5 2 1 5 1 5 3 5 5 5 5 5 5 5 1 5 3 5 5 5 5 5 2 5 2 5 5
    [38] 5 3 2 5 5 4 5 2 5 5 3 4 5 3 5 5 5 5 5 5 5 5 5 4 5 3 5 5 3 5 1 4 4 5 5 5 5
    [75] 5 3 5 5 4 3 4 5 5 5 5 5 4 5 5 2 3 5 1 5 5 5 5 5 5 5
share|improve this answer
Roman, Thank you – Harry Wells Nov 1 '11 at 20:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.