如果年份是 x 或 y,则每隔一行跳过一次,否则,转到管道中的下一行代码

If the year is x or y, skip every other row, otherwise, just go to the next line of code in the pipe

我有七年的温度数据,每小时记录一次,持续大约 9 周。 然而,其中两年每半小时记录一次。 那两年,我只想使用每隔一行,因此每小时数据。

七年来我有112个窝。有188550行数据... 原始数据前几行:

> head(TempData)
# A tibble: 6 x 6
  Beach  Nest  Year Datetime             Temp NestID  
  <chr> <dbl> <dbl> <dttm>              <dbl> <fct>   
1 LB        1  2014 2014-01-12 09:00:00  27.2 LB1_2014
2 LB        1  2014 2014-01-12 10:00:00  27.2 LB1_2014
3 LB        1  2014 2014-01-12 11:00:00  27.2 LB1_2014
4 LB        1  2014 2014-01-12 12:00:00  27.2 LB1_2014
5 LB        1  2014 2014-01-12 13:00:00  27.2 LB1_2014
6 LB        1  2014 2014-01-12 14:00:00  27.1 LB1_2014

使用 if / else 语句给出以下警告:

Warning message:In if (.$Year == 2015 | .$Year == 2016) { : the condition has length > 1 and only the first element will be used

所以我尝试使用 ifelse 语句,但现在我收到一条错误消息:

Error in ifelse(., .$Year == 2015 | .$Year == 2016, subset(row_number()%%2 == : (list) object cannot be coerced to type 'logical'

任何人都可以提供任何其他建议/帮助吗?

这是我代码的开头:

  FloodedNestsIncSub = group_by (TempData, NestID, Nest, Year) %>%
  ifelse (.$Year == 2015 | .$Year == 2016, subset(row_number() %% 2 == 1) ) %>%
  mutate(TempDrop = Temp - lag(Temp, n=1, default = first(Temp))) %>%
  mutate(Flooded = TempDrop < -0.45)

如果我在“subset(row_number() %% 2 == 1”之后添加一个逗号并将其留空 - 否则只是转到下一行, 我得到一个不同的错误:

Error in ifelse(., .$Year == 2015 | .$Year == 2016, subset(row_number()%%2 ==  : 
  unused argument (alist())

我需要代码 如果年份是 2015 或 2016,则每隔一行跳过,然后转到下一行代码; 否则,只需转到下一行代码 ...

如果每 30 分钟测量一次,您可以过滤掉每 30 分钟一次的测量值。 这将删除一半。 可以这样做:

TempData %>% 
    # Only keep whole hours
    dplyr::filter(lubridate::minute(Datetime) == 0) %>%
    # Rest of pipe
    dplyr::mutate(
        TempDrop = Temp - dplyr::lag(Temp, n=1, default = first(Temp)),
        Flooded = TempDrop < -0.45
    )