【问题标题】:How to use RowMeans function combining with agrregate如何结合聚合使用 RowMeans 函数
【发布时间】:2017-12-01 18:04:47
【问题描述】:

我需要一张图表来显示一堆支持请求的平均解决时间。我得到了一个包含所有请求的数据框,每一行都是一个不同的请求,每一列代表一个不同的字段,如月份、优先级、组、解决时间等......

我想从该表中获取每个月的平均解决时间。我一直在尝试不同的方法,但到目前为止我还没有做到。我想我现在非常接近。这是我的代码:

df_tresolucion <- setNames(aggregate(tabla_tiempos$Ticket,by=list(tabla_tiempos$Metrica),FUN = rowMeans(tabla_tiempos[,10,drop=FALSE])),c("Metrica","Promedio"))

tabla_tiempos 是我之前提到的包含所有支持请求的数据框:

Ticket <- c('119237','119239','120598','120600','120612', '120615')
 Metrica <- c('apr 2017','may 2017', 'may 2017','may 2017','jun 2017','jun 2017')
Pais <- c('ESP','ESP','ESP','ESP','ESP','ESP')Estado <- c('Closed', 'Closed', 'Closed', 'Closed', 'Closed', 'Closed')
Escalado <- c('CEDEX', 'CEDEX', 'TAC','CEDEX','CEDEX','TAC')
Vendor <- c('Cisco','Cisco','Cisco','Juniper','Cisco','Cisco')
Familia <- c('C7600','C7600','MX960','C7600','C7600','MX960')
Severidad <- c('Minor','Minor','Major','Minor','Minor','Major')
SLA <- c('Dentro de SLA','Dentro de SLA','Dentro de SLA','Dentro de SLA','Dentro de SLA','Dentro de SLA')
tresolucion <- c ('1.73','2.30','26.97','1.73','2.30','26.97')
tabla_tiempos <- data.frame(Ticket,Metrica,Pais,Estado,Vendor,Familia,Severidad,SLA,tresolucion)

$Ticket 是每个请求的唯一字段,$Metrica 表示打开请求的月份。数据框中的第 10 列 tabla_tiempos 包含解决时间。我使用参数 DROP = FALSE 来保留尺寸,以便我可以应用函数 RowMeans。

不幸的是,我收到了这个错误,我不太明白这是什么意思:

Warning: Error in match.fun: 'rowMeans(tabla_tiempos[, 10, drop = FALSE])' is not a function, character or symbol

正如我之前所说,我想按月汇总并计算每个月的平均解决时间,然后创建另一个包含所有这些信息的数据框(这将是 df_tresolucion):2列(月和平均时间)和每个月的一行。

有人知道我该怎么做吗?提前致谢!

【问题讨论】:

标签: r dataframe mean


【解决方案1】:

下面是使用dplyr 的方法。请注意,我从您的 data.frame 中删除了因子,并将 tresolucion 强制为数字。

library(dplyr)
tabla_tiempos%>%
  group_by(Metrica)%>%
  summarise(tresolucion=mean(tresolucion,na.rm=TRUE))

   Metrica tresolucion
     <chr>       <dbl>
1 apr 2017     1.73000
2 jun 2017    14.63500
3 may 2017    10.33333

数据

Ticket <- c('119237','119239','120598','120600','120612', '120615')
Metrica <- c('apr 2017','may 2017', 'may 2017','may 2017','jun 2017','jun 2017')
Pais <- c('ESP','ESP','ESP','ESP','ESP','ESP')
Estado <- c('Closed', 'Closed', 'Closed', 'Closed', 'Closed', 'Closed')
Escalado <- c('CEDEX', 'CEDEX', 'TAC','CEDEX','CEDEX','TAC')
Vendor <- c('Cisco','Cisco','Cisco','Juniper','Cisco','Cisco')
Familia <- c('C7600','C7600','MX960','C7600','C7600','MX960')
Severidad <- c('Minor','Minor','Major','Minor','Minor','Major')
SLA <- c('Dentro de SLA','Dentro de SLA','Dentro de SLA','Dentro de SLA','Dentro de SLA','Dentro de SLA')
tresolucion <- as.numeric(c('1.73','2.30','26.97','1.73','2.30','26.97'))
tabla_tiempos <- data.frame(Ticket,Metrica,Pais,Estado,Vendor,Familia,Severidad,SLA,tresolucion,stringsAsFactors = FALSE)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-19
    • 2018-10-20
    • 2014-01-21
    • 1970-01-01
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多