我有一个网络流量数据,用于为r DataSet中的十天段的每个小时。

   Day   Hour         Volume          Category
    0    00            100            P2P
    0    00            50             email
    0    00            200            gaming
    0    00            200            video
    0    00            150            web
    0    00            120            P2P
    0    00            180            web
    0    00            80             email
    ....
    0    01            150            P2P
    0    01            200            P2P
    0    01             50            Web
    ...
    ...
    10   23            100            web
    10   23            200            email
    10   23            300            gaming
    10   23            300            gaming
.

所见,在单个小时内有重复类别。我需要计算这些不同应用类别的平均小时比率的波动率和峰值小时。

volatility :每小时卷的标准偏差除以每小时平均。

高峰小时到avg。小时比率:最大时间的体积比率为Vol。该应用程序的平均小时。

所以我如何聚合和计算每个类别的这两个统计数据?我是r的新手,并没有对如何聚合并获得平均值的知识。

所以,最终结果看起来像这样的东西,其中首先通过概括卷,然后计算两个统计量来在单个24小时内聚合在一个24小时内聚合。

Category    Volatility      Peak to Avg. Ratio
Web            0.55            1.5
P2P            0.30            2.1
email          0.6             1.7
gaming         0.4             2.9
.

编辑:Plyr达到我。

stats = ddply(
    .data = my_data
    , .variables = .( Hour , Category)
    , .fun = function(x){
        to_return = data.frame(
            volatility = sd((x$Volume)/mean(x$Volume))
            , pa_ratio = max(x$Volume)/mean(x$Volume)
        )
        return( to_return )
    }
)
.

但这不是我希望的。我希望每类的统计数据,通过概括卷,然后计算挥发性,并计算出挥发性和PA比率,将所有时间的数小时汇总到24小时内。任何改进的建议?

有帮助吗?

解决方案

You'd need to do it in two stages (using the plyr package): First, as you pointed out, there can be multiple Day-Hour combos for the same category, so we first aggregate, for each category, its totals within each Hour, regardless of the day:

df1 <- ddply( df, .(Hour, Category), summarise, Volume = sum(Volume))

Then you get your stats:

> ddply(df1, .(Category), summarise,
+            Volatility = sd(Volume)/mean(Volume),
+            PeakToAvg = max(Volume)/mean(Volume) )

  Category Volatility PeakToAvg
1      P2P  0.3225399  1.228070
2      Web         NA  1.000000
3    email  0.2999847  1.212121
4   gaming  0.7071068  1.500000
5    video         NA  1.000000
6      web  0.7564398  1.534884
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top