I'm an R noob, please bear with me.

I'm using R to plot data in a scatterplot along with 2 gridlines separating the plot into 4 quadrants. I'd like to count the number of data points in each quadrant. How might I do this?


closed as off-topic by Michael Chernick, whuber Jan 4 '18 at 0:07

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – Michael Chernick, whuber
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ Beauty! @hxd1011 $\endgroup$ – Spagooters Jan 4 '18 at 0:23

The table function in R provide us a simpler way of doing the same thing as @Mark White suggested.

A simpler code table(x>0, y>0) will build a 2D table of the on four quadrants.


Once you define what the values determining the quadrants are (here, x = 0 and y = 0), you can use the operators <, >, and & to get values that are TRUE or FALSE if the case lies in that quadrant. Since R stores TRUE as 1 and FALSE as 0, you can simply sum across these to see how many are in each quadrant. Try this:

x <- rnorm(100)
y <- rnorm(100)

plot(x, y)
abline(h = 0)
abline(v = 0)

quadrant_1 <- sum((x < 0) & (y < 0))
quadrant_2 <- sum((x > 0) & (y < 0))
quadrant_3 <- sum((x < 0) & (y > 0))
quadrant_4 <- sum((x > 0) & (y > 0))
  • $\begingroup$ +1 for answering most basic (and probably off topic) question. $\endgroup$ – hxd1011 Jan 3 '18 at 21:14
  • $\begingroup$ @hxd1011 off-topic, but fun and simple question $\endgroup$ – Mark White Jan 3 '18 at 21:15
  • 1
    $\begingroup$ 'table(x>0, y>0) most R way of doing it :) $\endgroup$ – hxd1011 Jan 3 '18 at 21:17
  • $\begingroup$ haha, darn it, that's... a much better way of coding it... $\endgroup$ – Mark White Jan 3 '18 at 21:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.