ddply函数基于两个变量

时间:2016-02-14 22:20:58

标签: r

如何修改以下代码以计算每个Type级别的每个ID的平均时间。

 df <- structure(list(ID = structure(c(1L, 2L, 2L, 3L, 3L, 4L, 4L, 4L, 
              4L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 5L, 6L), .Label = c("R1", "R2", 
              "R3", "R4", "R5", "R6"), class = "factor"), cost = c(123L, 12L, 
              14L, 15L, 16L, 17L, 18L, 9L, 6L, 6L, 7L, 8L, 1L, 111L, 121L, 
              141L, 181L, 1611L), Time = c(123L, -12L, NA, -15L, NA, 17L, 18L, 
             -9L, 88L, 666L, 7L, 78L, 188L, 111L, 121L, 141L, 189L, 161L), 
              number = c(14L, 12L, 15L, 17L, NA, 17L, 22L, 95L, NA, 67L, 
              7L, 8L, 7L, 118L, NA, 140L, 180L, 1611L),type = c("A", "A", "B", "B", "B", "C", "C", 
             "M", "A", "M", "D", "D", "C", "A", "D", "B", "B", "M")), .Names = c("ID", 
              "cost", "Time", "number","type"), row.names = c(NA, -18L), class = "data.frame")
     library(dplyr)
    df %>%
       group_by(id) %>%
       summarise(N.Time = sum(!is.na(Time)),
                 Time_Average = round(mean(Time[Time >=0 & !is.na(Time)]), 2))

0 个答案:

没有答案