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
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 drawmutate
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