Using the dplyr
package:
df %>%
group_by(name) %>%
filter(n() < 4)
# A tibble: 5 x 2
# Groups: name [2]
name x
<fct> <int>
1 a 1
2 a 2
3 a 3
4 b 4
5 b 5
n()
returns the number of observations in the current group, so we can group_by
name, and then keep only those rows which are part of a group where the number of rows in that group is less than 4.
Related Contents:
- Filter data.frame rows by a logical condition
- Subset / filter rows in a data frame based on a condition in a column
- Reshaping data.frame from wide to long format
- How to sum a variable by group
- How do I make a list of data frames?
- Aggregate / summarize multiple variables per group (e.g. sum, mean)
- Sort (order) data frame rows by multiple columns
- Numbering rows within groups in a data frame
- The difference between bracket [ ] and double bracket [[ ]] for accessing the elements of a list or dataframe
- Simultaneously merge multiple data.frames in a list
- Split data frame string column into multiple columns
- Calculate the mean by group
- Select the row with the maximum value in each group
- Count number of rows within each group
- Quickly reading very large tables as dataframes
- Remove rows with all or some NAs (missing values) in data.frame
- Drop unused factor levels in a subsetted data frame
- Split data.frame based on levels of a factor into new data.frames
- Select rows from a data frame based on values in a vector
- Combine a list of data frames into one data frame by row
- Why is `[` better than `subset`?
- Combine two data frames by rows (rbind) when they have different sets of columns
- Counting unique / distinct values by group in a data frame
- Pass a data.frame column name to a function
- Drop data frame columns by name
- How to split a data frame?
- Why is it not advisable to use attach() in R, and what should I use instead?
- Create an empty data.frame
- How to subset matrix to one column, maintain matrix data type, maintain row/column names?
- How to plot all the columns of a data frame in R
- Sample random rows in dataframe
- Opposite of %in%: exclude rows with values specified in a vector
- Extracting specific columns from a data frame
- How does one reorder columns in a data frame?
- How to drop columns by name in a data frame
- Subset dataframe by multiple logical conditions of rows to remove
- Filtering a data frame on a vector [duplicate]
- Rename multiple columns by names
- Convert data.frame column format from character to factor
- How do I extract a single column from a data.frame as a data.frame?
- Combine two or more columns in a dataframe into a new column with a new name
- Subset rows in a data frame based on a vector of values
- Re-ordering factor levels in data frame [duplicate]
- Subset data frame based on multiple conditions [duplicate]
- Filter data frame rows based on values in vector
- Using grep to help subset a data frame
- Should I use a data.frame or a matrix?
- Convert a dataframe to a vector (by rows)
- Dollar sign before a variable
- Selecting columns in R data frame based on those *not* in a vector