【问题标题】:R - Bin according to factorR - 根据因子分类
【发布时间】:2019-07-30 10:42:47
【问题描述】:

我有一个数据集,我想为其中的每个组和类型组合运行 classIntervals(df$vol, 3, style="jenks")

数据看起来有点像这样。

data_sam <- data.frame( "group"=c( "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
  "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
  "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A"
  ), "type"=c( "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
  "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B",
  "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B" ), "index"=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59
  ), "vol"=c(52,272,374,408,498,480,451,644,715,659,820,713,810,676,840,589,594,998,782,483,351,494,377,261,637,379,706,530,619,724,333,189,246,82,39,85,159,143,125,118,79,39,110,190,264,101,70,46,0,27,71,69,172,464,132,0,156,167,142,45,51,10,0,14,67,20,2,12,1,0,6,2,2,17,22,7,0,2,9,5,12,15,7,0,12,18,4,3,12,9,12,13,14,8,9,11,10,5,4,1,4,10,4,4,3,5,5,1,3,0,2,3,2,4,2,3,3,0,0,1,1,1,0,0,1,1,2,0,1,1,0,1,1,0,0,1,0,0,0,0,1,2,0,1,1
  ))

我希望能够查看每个组类型的 bin 结果。

根据上面的数据,我运行classIntervals应该得到以下结果:

A 组 - A 型

style: jenks
one of 2,628 possible partitions of this variable into 3 classes
[0,190]     (190,530]   (530,998] 
53          17          16 

A组-B型

style: jenks
one of 66 possible partitions of this variable into 3 classes
[0,2]   (2,5]   (5,14] 
34      15      10 

有没有一种方法可以循环遍历 data_sam 中的组类型以获取垃圾箱?并且,理想情况下,将结果查看为按以下方式格式化的data.frame

group type count1 count2 count3 boundary1 boundary2 boundary3 A A 53 17 16 [0,190] (190,530] (530,998] A B 34 15 10 [0,2] (2,5] (5,14]

另外,我很高兴看到data_sam 上的每一行都附有每个组内的休息时间。

我不确定这里有什么可能,所以请告诉我。

【问题讨论】:

    标签: r dataframe cluster-analysis


    【解决方案1】:

    考虑bytapply 的面向对象包装器,用于对因子子集运行操作。具体来说,您可以在最后返回要绑定在一起的数据帧列表。

    下面从classIntervals 调用中提取返回值的brks 对象作为docs 提及,假定为命名向量,其中名称是边界,值是计数。

    df_list <- by(df, df[,c("group", "type")], function(sub) {
       tryCatch({
          res <- classIntervals(sub$vol, n=3, style="jenks")$brks
    
          data.frame(group = sub$group[1],
                     type = sub$type[1],
                     count1 = res[1],
                     count2 = res[2],
                     count3 = res[3],
                     boundary1 = names(res)[1],
                     boundary2 = names(res)[2],
                     boundary3 = names(res)[3])
          }, error = function(e) NA
       )
    })
    
    final_df <- do.call(rbind, df_list)
    

    【讨论】:

    • 我似乎遇到了错误。 data.frame(group= sub$group[1], type= sub$type[1], : arguments 暗示不同的行数: 1, 0 你能帮忙吗?
    • 如果有多个分组,by 会返回所有可能的匹配项,即使没有匹配项(即空数据框)。请参阅使用tryCatch 编辑以绕过这些情况。
    猜你喜欢
    • 2020-09-24
    • 2019-04-13
    • 2016-03-06
    • 2022-11-23
    • 2017-02-09
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多