I need to create data frame in R with Id and and gender of 50 employees. For that I used this code with gl()
function.
gender<-gl(2, 25, label=c("Male", "Female"))
id<-1:50
df <- data.frame(id, gender)
But the problem here is the first 25 rows are "male" and the next 25 rows are "female". I want to generate gender randomly with a 50% chance for each gender. Is there anyway to do it in R?
You probably want sample()
gender <- sample(c("Male", "Female"), size=50, replace=TRUE)
You can also do things like create a vector in random order with exactly 25 males and females
sample(rep(c("Male","Female"),each=25)
or get proportions other than 50/50
sample(c("Male", "Female"), size=50, replace=TRUE, prob=c(0.75,0.25))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.