What should I do if I want to devide data into 2 groups by certain percentile?

I want to do binary logistic regression analysis, but the value that I wanna use as dependent variable is continuous form. So I guess I have to code this dependent variable data into 2 groups, 1 and 0.
What I want to set as criterion of division is 30%. Thus, I want to code 1 if the datum(row) belongs to upper 30% among the whole data while 0 if the datum belongs to under 30% among the whole data.
I've searched how to code this. There are lots of example that coding with certain constant for just by quntiles/decile, but few of certain exact ratio. What should I do?
I would be very appreciate if you tell me an example, supposing the name of the data is "glm_example", and the variable that I wanna devide is "exposure".

Hi,

I want to code 1 if the datum(row) belongs to upper 30% among the whole data while 0 if the datum belongs to under 30% among the whole data.

I assume you mean 0 if the datum belongs to under 70% among the whole data.

glm_example <- data.frame(exposure=c(seq(1:10)))
q <- quantile(glm_example$exposure, .7)
glm_example$exposure_binary <- ifelse(glm_example$exposure <= q, 0, 1)
glm_example

exposure exposure_binary
1 1 0
2 2 0
3 3 0
4 4 0
5 5 0
6 6 0
7 7 0
8 8 1
9 9 1
10 10 1

1 Like

Thank youl. But it seems that you gave me an example under supposition there is a data composed of 1~10.
And I'm gonna analyze with my own data. In this situation I guess the first line wouldn't be needed, right?
Plus, isn't it right reversing the sequence of 0 and 1 on third line? Because if the logic is true, it returns 1, and if not it returns 0.

Hi.

I don't have your data, so I composed an example with my data. So yes, you don't need my first line.

You said you want to return 1 if the row belongs to the upper 30%. My rows 8, 9, and 10 are greater than the 70% quantile, which is the same as being in the upper 30%, and so return 1.

If this is not exactly what you want, does this provide enough of an example so that you can change it to what you want?

It was exactly helpful. I just wanted to confirm some details to apply to my code. Thank you so much

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.