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

后端 未结 3 618
孤独总比滥情好
孤独总比滥情好 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:03

    The package dplyr makes this kind of things easier.

    library(dplyr)
    mydata %>% group_by(id, day) %>% filter(row_number(value) == 1)
    

    This command requires more memory in R than in Stata: rows are not suppressed in place, a new copy of the dataset is created.

提交回复
热议问题