Yamuna_dhungana
Yamuna_dhungana

Reputation: 663

How to select range of values from scatter plot in R

I have a dataframe IBD for the plot below. I has columns Z1 and Z0.

enter image description here

I need to count the number of points in red circles.

For top and bottom, I did

# sum(IBD$Z0<0.5 & IBD$Z1<0.5) # bottom left corner --> indicates possible duplicates
## [1] 313
# sum(IBD$Z0<0.5 & IBD$Z1>0.5) #top left corner --> indicates possible Parent-offspring relatinships
## [1] 254

How do I get the number for middle circle?

Basically, I need to get the number for Z0 less than 0.5 and greater than 0.125 & Z1 less than 0.75 and greater than 0.25. How do I code this in R?

Upvotes: 0

Views: 169

Answers (1)

rjen
rjen

Reputation: 1972

You can add more conditions to your existing code. Please note that I just generated a bunch of numbers to get some data.

sum(data$Z0 < 0.5 &
    data$Z0 > 0.125 &
    data$Z1 < 0.75 &
    data$Z1 > 0.25)

# [1] 2

Or you can use dplyr.

library(dplyr)

data %>%
  filter(Z0 < 0.5 &
         Z0 > 0.125 &
         Z1 < 0.75 &
         Z1 > 0.25) %>%
  nrow()

# [1] 2

Data

library(dplyr)

data <- tibble(Z0 = rnorm(0.5, n = 100),
               Z1 = rnorm(0.5, n = 100))

Upvotes: 1

Related Questions