R delete rows in data frame where nrow of index is smaller than certain value

后端 未结 3 1717
眼角桃花
眼角桃花 2021-01-25 18:15

I want to delete certain rows in a data frame when the number of rows with the same index is smaller than a pre-specified value.

> fof.6.5[1:15, 1:3]
   draw          


        
3条回答
  •  囚心锁ツ
    2021-01-25 18:57

    Code below makes that:

    library(dplyr)
    fof.6.5 <- data.frame(draw = rep(1:4, c(5, 5, 3, 2)),
                          Fun.ID = rnorm(15),
                          Firm.ID = rnorm(15))
    fof_filter <- fof.6.5 %>% group_by(draw) %>% mutate(freq = n()) %>%
      filter(freq >= 5) %>% select(-freq)
    fof_filter
    
    • group_by join (in someway) profiles with the same value of draw
    • mutate add a new variable "freq" that give the number of profiles per each group of draw and repeats it.
    • filter selects only those profiles with "freq" greater or equals to 5
    • select delete the "freq" column because is not needed anymore

提交回复
热议问题