在r和求和值中创建班级间隔 [英] Create class intervals in r and sum values

查看:50
本文介绍了在r和求和值中创建班级间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组数据(成本和距离),我想根据距离汇总这ns个类,并找到汇总数据的成本总和。

I have a set of data (cost & distance) I want to aggregate those ns classes depending on the distance and find the sum of the cost for the aggregated data.

以下是一些示例表。

Nam Cost    distance
1   1005    10
2   52505   52
3   51421   21
4   651     10
5   656     0
6   5448    1

   Class From   To
        1   0   5
        2   5   15
        3   15  100

结果

Class   Sum
    1   6104
    2   1656
    3   103926

我正在这样做,但是要花很多时间。我确定有更好的方法

I am doing this but it takes a lot of time to process. I sure that there is a better way to do it

for (i in 1:6)
{
  for (j in 1:3)
  {
    if((Table_numbers[i,3]<=classes[j,2])& (Table_numbers[i,3]<classes[j,3]))
    {
      result_table[j,2]<-result_table[j,2]+ Table_numbers [i,2]
    } 

  }

}

我也使用classIntervals,但对于每个类我正在获取距离的计数,但是我需要成本的总和。

I used classIntervals as well but for each class I am getting the counts of the distance, but I need the sum of the cost.

我也尝试使用group_by,但是我不知道我是否可以使用类

I try to use group_by as well but i don't know if i can use classes for grouping.

您是否知道我该如何更有效?

Do you have any idea how I can do that more efficient?

推荐答案

这是结合 findInterval tapply

tapply(Table$Cost, findInterval(Table$distance, c(0, Classes$To)), sum)
#    1      2      3 
# 6104   1656 103926 

如果分类名称可能有所不同(不仅仅是计数器),您可以修改为

If Classes names may differ (not just a counter), you could modify to

tapply(Table$Cost, Classes$Class[findInterval(Table$distance, c(0, Classes$To))], sum)

这篇关于在r和求和值中创建班级间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆