I have seen similar questions, but couldn't apply them to solve my problem. I want a function to filter groups with less than 3 observations. As I wanted that for several dataframes, I need a function. I did it with dplyr and merge, but I would like a better code that uses only dplyr or datatable.
data <- read.table(text="
col1 col2
group1 some
group1 some2
group1 some3
group2 some
group2 some2",header=TRUE,fill=TRUE,stringsAsFactors=FALSE)
filter3 <- function(df, colgroup1) {
df %>%
group_by_(colgroup1) %>%
summarise_(Count = ~n()) #%>%
}
great3<-function(x, col){
x[x[,col] >=3 ,]
}
allfiltered<-function(df,colgroup1){
counts<-filter3(df,colgroup1)
final<-great3(counts,"Count")
merge(df,final, by=colgroup1)}
allfiltered(data,"col1")
#expected, count column dispensable, (function for 1 df or list of dfs wanted)
col1 col2 Count
1 group1 some 3
2 group1 some2 3
3 group1 some3 3