dplyr: filter rows according to aggregated function result

后端 未结 1 1842
天命终不由人
天命终不由人 2020-12-21 06:59

I have a table listing (amount, year and month) and I want to filter the rows corresponding to complete years. I.e. I want to ommit the last 4 rows of the sample dataframe I

1条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-21 07:09

    tally() is the equivalent of summarise(n=n()). However, in this case you want to keep the original rows of the data frame, but filtered so that rows that are part of incomplete years are removed. @AndresT's answer will work fine, but you can also do it more concisely without an intermediate step of creating a column to count the number of rows for each group:

    df %>% group_by(year) %>% filter(n()==12)
    

    0 讨论(0)
提交回复
热议问题