Reputation: 127
How can I randomly create categorical variables in R?
Below I randomly create a variable that is composed of the values "m" and "f", which stand for male and female.
sex <- sample(letters[c(6,13)], 10000, replace=TRUE)
But how can I enter the values in words, as in "male" and "female"?
Upvotes: 1
Views: 6006
Reputation: 488
An alternative approach is to create a vector of the string values, clearly specifying the categories to be randomly created, and then input this vector into the sample()
function. Specific proportions for the randomly generated data set can be defined here also, which can be helpful.
Example code:
STRINGS <- c("Male","Female")
STRINGS <- sample(STRINGS, 25, replace=TRUE, prob=c(0.5, 0.5))
Upvotes: 3