Cameron M
Cameron M

Reputation: 55

How to subset data above and below a line in R?

I have a data frame of x and y values.

I have a custom line defined with a slope and intercept. Not a regression.

How can I subset values from the data frame that are above the line?

I would like the make a new column in the data frame with a categorical variable that denotes "above line" and "below line".

Reproducible example:

set.seed(12)
x<-runif(100,min=1,max=700)
y<-runif(100,min=1,max=350)
df<-data.frame(x,y)

ggplot(df, aes(x=x,y=y)) +
  geom_point() +
  geom_abline(aes(intercept=187.835,slope=-0.309), color="red")

enter image description here

Upvotes: 0

Views: 228

Answers (1)

Ma&#235;l
Ma&#235;l

Reputation: 51974

Create an ifelse statement:

df <- df %>% 
  mutate(new = ifelse(y > 187.185 - 0.309*x, "above", "below"))

df %>%
  ggplot(aes(x=x,y=y,color = new)) +
  geom_point() +
  geom_abline(aes(intercept=187.835,slope=-0.309), color="red")

enter image description here

You can also filter using filter:

df %>%
  filter(new == "above")

Upvotes: 2

Related Questions