Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R检查并计算向量中的字符串,分组,考虑字符串的出现顺序_R_Dataframe_Group By_Count_Summarize - Fatal编程技术网

R检查并计算向量中的字符串,分组,考虑字符串的出现顺序

R检查并计算向量中的字符串,分组,考虑字符串的出现顺序,r,dataframe,group-by,count,summarize,R,Dataframe,Group By,Count,Summarize,数据采用以下格式,其中我必须使用日期对_进行分组。为了方便起见,我把它显示为数字 Msg <- c("Errors","Errors", "Start","Stop","Start","Stop","Errors","Errors","Start","Stop", "

数据采用以下格式,其中我必须使用日期对_进行分组。为了方便起见,我把它显示为数字

Msg <- c("Errors","Errors", "Start","Stop","Start","Stop","Errors","Errors","Start","Stop",
         "Stop" ,"Start","Errors","Start","Stop","Start" ,"Stop" ,
         "Errors", "Start","Errors","Stop", "Start", "LostControl","LostControl", "Errors",
         "Failed", "Stop", "Start","Failed","Stop","Stop","Start","Stop","Start","Error","Start",
         "Failed", "Stop")
Date <- c(11,11,11,11,11,11,11,12,12,12,12,12,12,14,14,14,14, 19,19,19,19,
        20,20,20,20,20,20,21,21,21,21,22,22,22,22,22,22,22)
data<- data.frame(Msg,Date)

Msg我们可以修改我昨天在你的

between_valid_anchors <- function(x, bgn = "Start", end = "Stop") {
  are_anchors <- x %in% c(bgn, end)
  xid <- seq_along(x)
  id <- xid[are_anchors]
  x <- x[are_anchors]
  start_pos <- id[which(x == bgn & c("", head(x, -1L)) %in% c("", end))]
  stop_pos <- id[which(x == end & c(tail(x, -1L), "") %in% c("", bgn))]
  if (length(start_pos) < 1L || length(stop_pos) < 1L)
    return(logical(length(xid)))
  xid %in% unlist(mapply(`:`, start_pos, stop_pos))
}
输出

`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 6 x 2
   Date   Msg
  <dbl> <int>
1    11     0
2    12     0
3    14     0
4    19     1
5    21     1
6    22     2
`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 7 x 2
   Date   Msg
  <dbl> <int>
1    11     0
2    12     0
3    14     0
4    19     0
5    20     0
6    21     1
7    22     1
输出

`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 6 x 2
   Date   Msg
  <dbl> <int>
1    11     0
2    12     0
3    14     0
4    19     1
5    21     1
6    22     2
`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 7 x 2
   Date   Msg
  <dbl> <int>
1    11     0
2    12     0
3    14     0
4    19     0
5    20     0
6    21     1
7    22     1
`summary()`解组输出(用`.groups`参数覆盖)
#一个tibble:7x2
枣味精
1    11     0
2    12     0
3    14     0
4    19     0
5    20     0
6    21     1
7    22     1

Thank:)不过在最后的总结中有一点变化,因为我只想计算失败的部分。因此我将其修改为`%>%summary(Msg=sum(Msg%in%c(“失败”))`)。再次感谢。还有一个小问题。如果在LostControl消息出现后出现失败消息,则不算作失败。也就是说,外观的顺序很重要。有没有办法考虑到这一点?我在数据框中添加了一行以显示此案例(日期20-添加最后一行)