如何使用摘要从现有数据框创建数据框

时间:2015-10-28 15:17:06

标签: r

我正在尝试创建单独的数据框,其中包括主机名数据的Avg,Max和第95百分位数。

  1. 表示Cpubusy,<2.表示UsedPercentMemory
  2. 数据框看起来像这样:

    Hostname   Avg   Max 95th Percentile
    Web01      10     90    92
    Web02      5      80    75
    
    dput(d)
    
    
    
    structure(list(Hostname = structure(c(8L, 8L, 9L, 5L, 6L, 7L, 
    1L, 2L, 3L, 4L), .Label = c("db01", "db02", "farm01", "farm02", 
    "tom01", "tom02", "tom03", "web01", "web03"), class = "factor"), 
        Date = structure(c(6L, 10L, 5L, 3L, 2L, 1L, 8L, 9L, 7L, 4L
        ), .Label = c("10/5/2015 1:15", "10/5/2015 1:30", "10/5/2015 2:15", 
        "10/5/2015 4:30", "10/5/2015 8:30", "10/5/2015 8:45", "10/6/2015 8:15", 
        "10/6/2015 8:30", "9/11/2015 5:00", "9/11/2015 6:00"), class = "factor"), 
        Cpubusy = c(31L, 20L, 30L, 20L, 18L, 20L, 41L, 21L, 29L, 
        24L), UsedPercentMemory = c(99L, 98L, 95L, 99L, 99L, 99L, 
        99L, 98L, 63L, 99L)), .Names = c("Hostname", "Date", "Cpubusy", 
    "UsedPercentMemory"), class = "data.frame", row.names = c(NA, 
    -10L))
    

    在r中有一种简单的方法可以做到这一点,我试图避免循环。

    我试过了:

    dd %>% group_by(Hostname) %>% summarise_each(funs(mean, max))
    

    我无法弄清楚第95百分位数。

1 个答案:

答案 0 :(得分:1)

不确定这是否是有效的方法

library(dplyr)
library(lazyeval)
dd %>% 
   group_by(Hostname) %>% 
   summarise_(Mean = interp(~mean(var, na.rm=TRUE), var=as.name(m)), 
              Max=interp(~max(var, na.rm=TRUE), var=as.name(m)), 
              Quantile= interp(~quantile(var, prob=0.95), var=as.name(m)))