dplyr filter multiple columns for unique value in r

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.r
library(dplyr)

df_unique <- df %>% distinct(A, B, .keep_all = TRUE)
69 chars
4 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.r
df_unique <- df %>% distinct(A, B, C, .keep_all = TRUE)
56 chars
2 lines

This will filter for unique combinations of values in columns "A", "B", and "C".

gistlibby LogSnag