Create separate object and assign its values to rows

Hello everyone,
This is my first query here,

I have a dataframe of 1000 obs. I want to create a separate object with character values to assign "male" for the first 500 obs and "female" for the other 500.

Welcome to the community!

You can create a object of length 1000 with first 500 as male and n
last 500 as female easily as follows:

object_name <- rep(x = c("male", "female"), each = 500)

Hope this helps.


Usually, a reproducible example is expected so that people here can visualise what do you have, what is the objective and what have you already tried. Though it is not a necessity for many questions, but it is a good practice and it is better to follow it. Please go through this guide for your next questions:

1 Like

Thanks mate!
out of curiosity, what if I want to assign "male" for the first 633 and "female" for the rest?

Best regards :slight_smile:

You're welcome.

I can help with you curiosity alright, but shouldn’t you have tried it out by yourself first? That'd have been more fun and helpful for you I believe.

Use the times argument like this:

object_name <- rep(x = c("male", "female"), times = c(633, (1000 - 633)))

Many thanks, I am new to R and promise will try harder :smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.