Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter for unique combination of columns from an R dataframe

I am trying to filter out the duplicates of a subset of columns from a dataframe in R.

I am interested in filtering unique combinations of session, first, and last. The following is what my data looks like

                        session       first     last            city 1  9cf571c8faa67cad2aa9ff41f3a26e38     cat   biddix          fresno 2  e30f853d4e54604fd62858badb68113a   caleb     amos                 3  2ad41134cc285bcc06892fd68a471cd7  daniel  folkers                 4  2ad41134cc285bcc06892fd68a471cd7  daniel  folkers                 5  63a5e839510a647c1ff3b8aed684c2a5 charles   pierce           flint 6  691df47f2df12f14f000f9a17d1cc40e       j    franz prescott+valley 7  691df47f2df12f14f000f9a17d1cc40e       j    franz prescott+valley 8  b3a1476aa37ae4b799495256324a8d3d  carrie mascorro            brea 9  bd9f1404b313415e7e7b8769376d2705    fred  morales       las+vegas 10 b50a610292803dc302f24ae507ea853a  aurora      lee                 11 fb74940e6feb0dc61a1b4d09fcbbcb37  andrew    price       yorkville 
like image 805
megv Avatar asked Dec 02 '11 22:12

megv


People also ask

How do I extract unique values from multiple columns in R?

Data Visualization using R Programming To extract unique values in multiple columns in an R data frame, we first need to create a vector of the column values but for that we would need to read the columns in matrix form. After that we can simply unique function for the extraction.

How do you count unique combinations in R?

To find the count of unique group combinations in an R data frame, we can use count function of dplyr package along with ungroup function.

How do I filter a Dataframe in multiple conditions in R?

Multiple conditions can also be combined using which() method in R. The which() function in R returns the position of the value which satisfies the given condition. The %in% operator is used to check a value in the vector specified.


1 Answers

The following should do it:

unique(df[,c('session','first','last')]) 

where df is your data frame.

like image 142
NPE Avatar answered Sep 21 '22 11:09

NPE