To filter for rows that have unique values in multiple columns using dplyr
, we can use the distinct()
function with the .keep_all = TRUE
argument.
Let's assume we have a data frame df
with columns "A", "B", "C", and "D" and we want to filter for unique combinations of values in columns "A" and "B". We can do the following:
main.r69 chars4 lines
This will create a new data frame df_unique
with only the unique combinations of values in columns "A" and "B", along with all the other columns in the original data frame.
If we want to filter for unique combinations of values in more than two columns, we can simply add additional arguments to the distinct()
function:
main.r56 chars2 lines
This will filter for unique combinations of values in columns "A", "B", and "C".
gistlibby LogSnag