使用dplyr

时间:2018-12-04 22:38:36

标签: r dplyr tidyverse

我正在研究一项研究,我们使用一个放在巢箱内的照相机来确定我们的研究物种何时产下第一个卵。有些相机不是很可靠,我想看看在下第一枚卵之前是否有连续的照片。这样我就不能确定这是第一个鸡蛋约会。有超过165,000张照片和200个以上的嵌套,因此我按嵌套框ID分组,将行过滤为至少有1个鸡蛋的行,然后使用slice函数选择包含数据的第一行。这是一个可重现的示例:

example <- structure(list(boxID = c("CA10", "CA10", "CA10", "CA10", "CA10", 
    "CA10", "CA10", "CA10", "CA10", "CA10", "CA10", "CA10", "CA10", 
    "CA10", "CA10"), visitType = c("Image", "Image", "Image", "Image", 
    "Image", "Image", "Image", "Image", "Image", "Image", "Image", 
    "Image", "Image", "Image", "Image"), day = c(25L, 25L, 25L, 26L, 
    26L, 26L, 27L, 27L, 27L, 28L, 28L, 28L, 29L, 29L, 29L), month = c("MAR", 
    "MAR", "MAR", "MAR", "MAR", "MAR", "MAR", "MAR", "MAR", "MAR", 
    "MAR", "MAR", "MAR", "MAR", "MAR"), year = c(2018, 2018, 2018, 
    2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 
    2018), timeChecked = c("02:59", "09:06", "15:13", "02:59", "09:07", 
    "15:14", "02:59", "09:07", "15:13", "02:58", "09:06", "15:12", 
    "02:58", "09:06", "15:12"), species = c("Empty", "Empty", "Empty", 
    "Empty", "Empty", "Empty", "Empty", "Empty", "American Kestrel", 
    "Empty", "American Kestrel", "American Kestrel", "American Kestrel", 
    "American Kestrel", "American Kestrel"), sexAdult = c(NA, NA, 
    NA, NA, NA, NA, NA, NA, "Female", NA, "Female", "Female", "Female", 
    NA, NA), numEggs = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    "1", "1", "1", "1", "1"), numNestlings = c(NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_), date = structure(c(17615, 
    17615, 17615, 17616, 17616, 17616, 17617, 17617, 17617, 17618, 
    17618, 17618, 17619, 17619, 17619), class = "Date")), class = c("tbl_df", 
    "tbl", "data.frame"), row.names = c(NA, -15L), .Names = c("boxID", 
    "visitType", "day", "month", "year", "timeChecked", "species", 
    "sexAdult", "numEggs", "numNestlings", "date"))

这是我必须找到至少有一个鸡蛋的第一行的代码:

example %>%
      mutate_at(vars(numEggs, numNestlings), na_if, 'unknown') %>% # remove unknowns and other values that should be NA
      select(boxID, date, numEggs, visitType) %>%
      group_by(boxID) %>%
      filter(numEggs > 0) %>%
      slice(1) 

我想在第一行之前先看5或10行,然后再放一个鸡蛋,以确保在该时间点之前有连续的数据。有没有办法使用slice或其他dplyr函数对此行建立索引?

2 个答案:

答案 0 :(得分:1)

这是一种方式。 match返回第一个numEggs > 0的位置,然后我们简单地从该位置获得另外的n_previous行。我们使用max(1, ...),这样如果第一个numEggs > 0 < n_previous的位置不会出错。

n_previous <- 5

example %>%
  mutate_at(vars(numEggs, numNestlings), na_if, 'unknown') %>% 
  select(boxID, date, numEggs, visitType) %>%
  group_by(boxID) %>%
  slice(max(1, match(TRUE, numEggs > 0) - n_previous):match(TRUE, numEggs > 0))

# A tibble: 6 x 4
# Groups:   boxID [1]
  boxID date       numEggs visitType
  <chr> <date>     <chr>   <chr>    
1 CA10  2018-03-26 <NA>    Image    
2 CA10  2018-03-27 <NA>    Image    
3 CA10  2018-03-27 <NA>    Image    
4 CA10  2018-03-27 <NA>    Image    
5 CA10  2018-03-28 <NA>    Image    
6 CA10  2018-03-28 1       Image  

答案 1 :(得分:1)

这是一种根据numEggs的第一个非缺失值的位置进行切片的方法。您可以根据要保留的第一个非NA numEggs

之前的行,修改最后一行内的5值。
example %>%
      mutate_at(vars(numEggs, numNestlings), na_if, 'unknown') %>%
      select(boxID, date, numEggs, visitType) %>%
      group_by(boxID) %>%
      slice((min(which(!is.na(numEggs)))-5):min(which(!is.na(numEggs))))

    # A tibble: 6 x 4
# Groups:   boxID [1]
  boxID date       numEggs visitType
  <chr> <date>     <chr>   <chr>    
1 CA10  2018-03-26 NA      Image    
2 CA10  2018-03-27 NA      Image    
3 CA10  2018-03-27 NA      Image    
4 CA10  2018-03-27 NA      Image    
5 CA10  2018-03-28 NA      Image    
6 CA10  2018-03-28 1       Image