首页 文章

计算R中的故障率和日期时间操作

提问于
浏览
3

我有一个我正在使用的示例数据框

Datetime <- c("2015-09-29 08:22:00", "2015-09-29 09:45:00", "2015-09-29 09:53:00", "2015-09-29 10:22:00", "2015-09-29 10:42:00",
                  "2015-09-29 11:31:00", "2015-09-29 11:47:00", "2015-09-29 12:45:00", "2015-09-29 13:11:00", "2015-09-29 13:44:00",
                  "2015-09-29 15:24:00", "2015-09-29 16:28:00", "2015-09-29 20:22:00", "2015-09-29 21:38:00", "2015-09-29 23:34:00")
Measurement <- c("Length","Length","Width","Height","Width","Height","Length","Width","Width","Height","Width","Length",
                     "Length","Height","Height")
PASSFAIL <- c("PASS","PASS","FAIL","PASS","PASS","FAIL_AVG_HIGH","FAIL#Pts","FAIL","FAIL_AVG_LOW","FAIL","PASS","PASS","FAIL#RNG#HIGH","PASS","FAIL")

df1 <- data.frame(Datetime,Measurement,PASSFAIL)

DF1

Datetime Measurement      PASSFAIL
1  2015-09-29 08:22:00      Length          PASS
2  2015-09-29 09:45:00      Length          PASS
3  2015-09-29 09:53:00       Width          FAIL
4  2015-09-29 10:22:00      Height          PASS
5  2015-09-29 10:42:00       Width          PASS
6  2015-09-29 11:31:00      Height FAIL_AVG_HIGH
7  2015-09-29 11:47:00      Length      FAIL#Pts
8  2015-09-29 12:45:00       Width          FAIL
9  2015-09-29 13:11:00       Width  FAIL_AVG_LOW
10 2015-09-29 13:44:00      Height          FAIL
11 2015-09-29 15:24:00       Width          PASS
12 2015-09-29 16:28:00      Length          PASS
13 2015-09-29 20:22:00      Length FAIL#RNG#HIGH
14 2015-09-29 21:38:00      Height          PASS
15 2015-09-29 23:34:00      Height          FAIL

我正在研究一个有趣的问题,以便在一天中的12 AM-12 PM和12 PM-12 AM(第二天)找到每次测量的失败率 .

注意:在df1中,PASSFAIL列中具有FAIL的任何内容都被视为失败 .

Fail Rate = (Number of Fails)/(Number of Fails + Number of Pass)

我想要的输出是这样的

Datetime FailRate_length Total_length FailRate_Width Total_Width FailRate_Height Total_Height
1 2015-09-29 00:00:00 AM            0.33            3           0.50           2            0.50            2
2 2015-09-29 12:00:00 PM            0.50            2           0.66           3            0.66            3

我正在尝试使用dplyr和data.table包来解决这个问题,但我只是不知道如何在df1中划分时间间隔以获得具有2个值的df2 - > 12AM(df1的前7次观察)和12PM(The df1)中的下8个观测值 . 有人可以帮我吗?

2 回答

  • 5

    使用data.table ...

    library(data.table)
    
    # thanks to @DavidArenburg for suggesting this approach:
    
    df1[, `:=`( 
      d        = as.IDate(Datetime), 
      antepost = c("am","pm")[1+(hour(Datetime) >= 12)] ) 
    ]
    
    res <- setDT(df1)[ , .( 
      failrate    = sum(PASSFAIL != "PASS")/.N,
      N           = .N
    ), by = .(d, antepost, Measurement)]
    

    这使

    d antepost Measurement  failrate N
    1: 2015-09-29       am      Length 0.3333333 3
    2: 2015-09-29       am       Width 0.5000000 2
    3: 2015-09-29       am      Height 0.5000000 2
    4: 2015-09-29       pm       Width 0.6666667 3
    5: 2015-09-29       pm      Height 0.6666667 3
    6: 2015-09-29       pm      Length 0.5000000 2
    

    语法为 DT[i,j,by] ,其中 by 用于分组变量;和 j 用于处理列 . := := 内创建新列 .

    重塑OP的期望输出......

    dcast(res, d + antepost ~ Measurement, value.var = c("failrate", "N"))
    

    这使

    d antepost failrate_Height failrate_Length failrate_Width N_Height N_Length N_Width
    1: 2015-09-29       am       0.5000000       0.3333333      0.5000000        2        3       2
    2: 2015-09-29       pm       0.6666667       0.5000000      0.6666667        3        2       3
    

    感谢@Arun,这是一个完成所有操作的方法:

    dcast(setDT(df1), 
      as.IDate(Datetime) + c("am","pm")[1+(hour(Datetime) >= 12)] ~ Measurement, 
      value.var = "PASSFAIL", 
      fun.agg = list(function(x) sum(x != "PASS")/length(x), length)
    )
    

    这使

    Datetime Datetime_1 PASSFAIL_function_Height PASSFAIL_function_Length PASSFAIL_function_Width PASSFAIL_length_Height PASSFAIL_length_Length PASSFAIL_length_Width
    1: 2015-09-29         am                0.5000000                0.3333333               0.5000000                      2                      3                     2
    2: 2015-09-29         pm                0.6666667                0.5000000               0.6666667                      3                      2                     3
    

    列名称是从 ~ 部分中的根变量和每个函数定义的第一个字自动生成的 .

  • 3

    A dplyr + tidyr equivalent (略有不同的分箱,虽然上面的那个很优雅):

    library(plyr)
    library(dplyr)
    library(tidyr)
    
    df1 %>%
      mutate(
        half_day = 
          Datetime %>%
          as.POSIXct(tz = "UTC") %>%
          round_any(60*60*12, f = floor) ) %>%
      group_by(half_day, Measurement) %>%
      summarize(Total = n(),
             FailRate = sum(PASSFAIL != "PASS")/Total) %>%
      gather(variable, value, FailRate, Total) %>%
      unite(variable_new, variable, Measurement, sep = "_") %>%
      spread(variable_new, value)
    

    gatherunitespread 序列是 dcast 的tidyr等价物 . 注意

    半天*(12小时/半天)(60分钟/小时)(60秒/分钟)= 60 * 60 * 12秒

相关问题