【发布时间】:2016-05-21 01:51:50
【问题描述】:
这个问题是How can I sum rows that with non-numeric factor in R? 的扩展。我在 data.txt 中有数据框,如下所示:
Latency Port TrafficType Time
1 27821 Port1 ssh "2016/02/05 15:18:25"
2 24186 Port1 http "2016/02/05 15:18:25"
3 17963 Port1 ssh "2016/02/05 15:18:25"
4 20208 Port1 ftp "2016/02/05 15:18:25"
5 20703 Port2 ftp "2016/02/05 15:18:25"
6 29735 Port3 ssh "2016/02/05 15:18:25"
7 20975 Port1 https "2016/02/05 15:18:25"
8 29489 Port1 ssh "2016/02/05 15:18:25"
9 19319 Port4 ssh "2016/02/05 15:18:25"
10 18224 Port1 ssh "2016/02/05 15:18:25"
11 17952 Port1 ftp "2016/02/05 15:18:25"
12 17972 Port1 ssh "2016/02/05 15:18:25"
13 17300 Port1 ssh "2016/02/05 15:18:25"
14 20937 Port1 ssh "2016/02/05 15:18:25"
15 18769 Port1 ssh "2016/02/05 15:18:25"
16 18104 Port2 ssh "2016/02/05 15:18:25"
17 17496 Port2 ssh "2016/02/05 15:18:26"
18 23268 Port1 https "2016/02/05 15:18:26"
19 19457 Port1 ssh "2016/02/05 15:18:26"
20 20937 Port1 ssh "2016/02/05 15:18:25"
21 18769 Port1 ssh "2016/02/05 15:18:25"
22 18104 Port2 ssh "2016/02/05 15:18:25"
23 17496 Port2 ssh "2016/02/05 15:18:26"
24 23268 Port1 https "2016/02/05 15:18:26"
25 19457 Port1 ssh "2016/02/05 15:18:27"
....
我用tapply() 做了一些统计:
data <- read.table("data.txt")
fact <- factor(data$Port)
lat <- tapply(data$Latency, fact,
function(x) {
c(max(x),
mean(x),
median(x),
quantile(x, c(0.90,0.99,0.9999)))
})
然后我得到:
$Port1
90% 99% 99.99%
29489.00 20941.78 19832.50 25276.50 29205.44 29486.16
$Port2
90% 99% 99.99%
20703.00 18380.60 18104.00 19663.40 20599.04 20701.96
$Port3
90% 99% 99.99%
29735 29735 29735 29735 29735 29735
$Port4
90% 99% 99.99%
19319 19319 19319 19319 19319 19319
我想在上表中添加更多统计信息,如下所示:
$Port1
90% 99% 99.99% ftp http https ssh peak
29489.00 20941.78 19832.50 25276.50 29205.44 29486.16 2 1 3 12 14
$Port2
90% 99% 99.99% ftp http https ssh peak
20703.00 18380.60 18104.00 19663.40 20599.04 20701.96 1 0 0 4 3
$Port3
90% 99% 99.99% ftp http https ssh peak
29735 29735 29735 29735 29735 29735 ? ? ? ? ?
$Port4
90% 99% 99.99% ftp http https ssh peak
19319 19319 19319 19319 19319 19319 ? ? ? ? ?
昨天,我在How can I sum rows that with non-numeric factor in R? 中提问,感谢@akrun 教我一种方法,将table() 函数应用于数据子集以获取所有流量类型的计数:
t <- table(data[c("Port", "TrafficType")])
t
TrafficType
Port ftp http https ssh
Port1 2 1 3 12
Port2 1 0 0 4
Port3 0 0 0 1
Port4 0 0 0 1
现在,我的问题是:
如何将此结果附加到表中(在 99.99% 列之后)?
如何计算每个端口的峰值流量(流量/秒)?即,Port1 在 2016/02/05 15:18:25 有 14 个流量,在 2016/02/05 15:18:26 有 3 个流量,在 2016/02/05 15:18:27 有 1 个流量,所以它的峰值,I在这个地方需要一个数字 14。
希望我把我的问题描述得足够清楚。非常感谢您的耐心和友好的回复。
更新: 我发现了一个丑陋的方法,那就是单独计算味精率:
rate_df <- as.data.frame(data[c("Port", "Time")])
rate_fc <- factor(rate_df$Port)
peak <- tapply(rate_df$Freq, rate_fc, max) # <-
然后使用打印功能在延迟后附加峰值的值。它看起来很丑陋。这里需要专家的建议。非常感谢。
【问题讨论】:
-
修改你的匿名函数调用。
-
@Alex,完全不知道怎么做,刚开始学习 R 几个星期。
-
@LukeHuang Alex 所指的匿名函数调用是您的第二个代码块中对
function的调用。您可以在此处阅读有关匿名函数的更多信息:adv-r.had.co.nz/Functional-programming.html。 Alex 的建议是在创建行时将这些统计信息添加到行中。 -
谢谢@user164385 我明白你的意思,并会尝试。
-
@LukeHuang SO 不是编程服务。这是一个供程序员作为社区工作的问答网站。用户将努力学习编程是理所当然的。根据您的 cmets,我不相信您正在尝试。花点时间了解什么是匿名函数以及如何使用和修改它们。
标签: r