【问题标题】:How to count how many data point inside a region using 'segments'?如何使用“段”计算区域内有多少数据点?
【发布时间】:2017-10-24 01:13:56
【问题描述】:

有没有办法找出有多少数据点落在 R 的指定区域内?例如,这是我的代码:

data = read.csv("data.csv")
plot(data$X, data$Y, ylab = "Y", xlab = "X", pch = 1, col = unclass(data$classes), cex = 0.5, cex.axis = 0.5, main = "Y vs X Plot")
par(fig = c(0, 1, 0, 1), oma = c(0, 0, 0, 0), mar = c(0, 0, 0, 0), new = TRUE)
segments(-0.02, 0.36, 0.15, 0.36, col = c("purple"), lty = 1, lwd = 1)
segments(0.15, 0.35, 0.15, 0.9, col = c("purple"), lty = 1, lwd = 1)

这给了我这个情节:

Plot of Y vs X

我想知道有多少个红色圆圈落在左侧的矩形区域(带有紫色边框的那个)或位于其边界上。有没有办法在 R 上做到这一点?

【问题讨论】:

  • sum(data$X <= 0.15 & data$Y >= 0.36 & unclass(data$classes) == 2)
  • @Axeman 谢谢!这行得通!

标签: r csv plot


【解决方案1】:

不一定使用segments() 本身,但这里有一种使用数字本身的方法。

我没有你的数据,所以这里有一些假数据:

x <- rnorm(100)
y <- rnorm(100)
class <- sample(1:2, 100, replace=T)
plot(x,y, col=class)

# region we're interested in
xmin <- 0
xmax <- 1
ymin <- 0
ymax <- 1
rect(xmin, ymin, xmax, ymax)  
# rect was easier for what I wanted to show, but you can use numbers the same way

方框内有多少个点?

sum(x>xmin & x<xmax & y>ymin & y<ymax & class==2)
# [1] 11
# your results will vary

框的边框上有多少个点?

sum(((x>xmin & x<xmax & y==ymin) | (x>xmin & x<xmax & y==ymax) | (y>ymin & y<ymax & x==xmin) | (y>ymin & y<ymax & x==xmax)) & class==2)
# [1] 0
# again, your results will vary

【讨论】:

  • 谢谢,这也是一个好方法!就像@Axeman 建议的那样,符号 = 可以稍微简化代码。
【解决方案2】:

一种方法是操作现有数据框中的列。

library(ggplot2)
library(dplyr)
library(reshape2)
x <- runif(100, 0, 1)
y <- runif(100, 0, 1)
class <- round(runif(100, 0, 1))
df <- data.frame(cbind(x, y, class)) ## plot - insanity check 
ggplot(df, aes(x, y , color = as.character(class))) + geom_point()

## here x limits from 0 to 0.15 and y limits from 0 to 0.36

df <- df %>% mutate(div = ifelse(x < 0.15 & y < 0.36, 'Y', 'N'))
df %>% group_by(div, class) %>% summarise(cc =n()) %>% dcast(., div ~ class)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-12
    • 1970-01-01
    • 2017-11-01
    • 2016-06-29
    • 2019-11-13
    • 1970-01-01
    相关资源
    最近更新 更多