R - Keep first observation per group identified by multiple variables (Stata equivalent “bys var1 var2 : keep if _n == 1”)

后端 未结 3 622
孤独总比滥情好
孤独总比滥情好 2020-12-15 00:27

So I currently face a problem in R that I exactly know how to deal with in Stata, but have wasted over two hours to accomplish in R.

Using the data.frame below, the

3条回答
  •  被撕碎了的回忆
    2020-12-15 01:09

    Using data.table, assuming the mydata object has already been sorted in the way you require, another approach would be:

    library(data.table)
    mydata <- data.table(my.data)
    mydata <- mydata[, .SD[1], by = .(id, day)]
    

    Using dplyr with magrittr pipes:

    library(dplyr)
    mydata <- mydata %>%
      group_by(id, day) %>%
      slice(1) %>%
      ungroup()
    

    If you don't add ungroup() to the end dplyr's grouping structure will still be present and might mess up some of your subsequent functions.

提交回复
热议问题