【问题标题】:Partioning points into equal groups from their average将点从它们的平均值分成相等的组
【发布时间】:2020-07-22 07:38:24
【问题描述】:

我有一组像这样的点,我正在尝试根据它们与平均值的距离将它们分组或聚类为大小相等的四组。

df <- data.frame(x = rnorm(50, 0, 1),
                 y = rnorm(50, -0, 0.7))

ggplot(data = df, aes(x = x, y = y)) +
  geom_point() +
  geom_abline(slope = 0.7, intercept = 0) +
  geom_abline(slope = -0.7, intercept = 0)

在这种情况下,我希望根据每个点相对于平均值的距离(此处为 (0,0))以及该距离的趋势是向上、向下、向左还是向右,为每个点分配一个组.感谢您的帮助。

【问题讨论】:

    标签: r group-by cluster-analysis partitioning partition


    【解决方案1】:
    
    df <- data.frame(x = rnorm(50, 0, 1),
                     y = rnorm(50, -0, 0.7),
                     center = rep(0,50))  # not really needed 
    df$v1 <- -df$x^2     # vector one 0(center) - df$x2
    df$v2 <- -df$y^2     # vector two 0(center) - df$y2
    df$dist = df$v1 + df$v2  
    df$len = sqrt(abs(df$dist)) #lenght of vector betwen center and point
    df$group = cut(df$len, 10,labels = paste("Level",LETTERS[1:10])) # grouping into 10 bins
    
    df<-df %>% mutate(posit= case_when(x < mean(x) & y < mean(y) ~ "A", 
                                  x > mean(x) & y  > mean(y) ~ "B",
                                  x <mean(x) & y  >mean(y) ~ "C",
                                  x >mean(x) & y  <  mean(y) ~ "D"))             
    
    ggplot(data = df ) +
      geom_point(aes(x = x, y = y,col= group, shape= posit, size=2)) +
      geom_vline(xintercept =mean(df$x))+
      geom_abline(slope = -0.0, intercept =mean(df$y))
    

    【讨论】:

    • 这很有帮助,谢谢!你知道这个分界线是如何对角线进行的吗?我正在尝试将数据拆分为更接近图表中的外观?
    猜你喜欢
    • 1970-01-01
    • 2019-02-12
    • 1970-01-01
    • 1970-01-01
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    相关资源
    最近更新 更多