Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
974 views
in Technique[技术] by (71.8m points)

group by - flagging every 6 rows in R if there are two (1) within them


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It seems you are new to SO. This is a good and important read which will make sure you attract people to work on your Question.

Here is a solution for how I understood your question. A time sequence over one year is created in steps of 10 minutes.

Vector check has sampled 0 or 1 over the length of interval. For reproducability I have set a seed.

A DF is made out of both. This DF is grouped for month, day and hour and a value is created which sums the 1 's per hour. If this number is 2 or greater a new variable flag gets an NA , if not it gets an empty string.

At the end only the relevant variables are selected.

library(tidyverse)
library(lubridate)

set.seed(1)
interval <- seq(ymd_hms('2020-12-01 00:00:00'), 
                by = '10 min',length.out=(60*24*365/10))

check <- sample(c(0,1), length(interval), replace = T)
df <- data.frame(interval, check)

df %>% 
  mutate(hour = hour(interval)) %>% 
  group_by(month(interval),day(interval), hour(interval)) %>% 
  mutate(N = sum(check)) %>% 
  mutate(flag = ifelse(N >= 2, NA, '')) %>% 
  ungroup() %>% 
  dplyr::select(interval, check,N, flag) 
#> # A tibble: 52,560 x 4
#>    interval            check     N flag 
#>    <dttm>              <dbl> <dbl> <chr>
#>  1 2020-12-01 00:00:00     0     2 <NA> 
#>  2 2020-12-01 00:10:00     1     2 <NA> 
#>  3 2020-12-01 00:20:00     0     2 <NA> 
#>  4 2020-12-01 00:30:00     0     2 <NA> 
#>  5 2020-12-01 00:40:00     1     2 <NA> 
#>  6 2020-12-01 00:50:00     0     2 <NA> 
#>  7 2020-12-01 01:00:00     0     2 <NA> 
#>  8 2020-12-01 01:10:00     0     2 <NA> 
#>  9 2020-12-01 01:20:00     1     2 <NA> 
#> 10 2020-12-01 01:30:00     1     2 <NA> 
#> # … with 52,550 more rows

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...