dimanche 10 janvier 2021

How do you randomly generate a dataset with categorical variables in R?

There has been a question asked previously in the forum on something similar, however it doesn't quite go into enough detail for my issue.

I'm trying to generate a random dataset with 8 columns and 50,000 rows. Each column must be a categorical variable (my example, breed of dog) with 3 levels (example, colour), resulting in approx. equal proportions.

The line of code below does exactly what I'm looking for, but how do you alter this to fit the following values?

breeds of dog, instead of "8" - (Poodle, Labrador, Pug, Chihuahua, Collie, Shitzu, Bulldog, Lurcher)

colours, instead of "LETTERS" - (brown, black, white)

df <- data.frame(replicate(8,sample(LETTERS[1:3], 50000, replace = TRUE)))

Any help is much appreicated, thank you.




Aucun commentaire:

Enregistrer un commentaire