如何使用与agrregate结合的RowMeans函数

时间:2017-06-28 11:40:15

标签: r dataframe mean

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

从那张桌子我想得到每个月的平均解决时间。我一直在尝试不同的方式,但到目前为止我还没有。我想我现在非常接近。这是我的代码:

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 < / strong>包含解决时间。我使用参数 DROP = FALSE 来保留尺寸,以便我可以应用RowMeans函数。

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

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

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

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

1 个答案:

答案 0 :(得分:0)

以下是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)