How to find unique field values ​​from two columns in a data frame

I have a data frame containing many columns, including Quarter and CustomerID . In this, I want to highlight the unique combination of Quarter and CustomerID .

For instance,

 masterdf <- read.csv(text = " Quarter, CustomerID, ProductID 2009 Q1, 1234, 1 2009 Q1, 1234, 2 2009 Q2, 1324, 3 2009 Q3, 1234, 4 2009 Q3, 1234, 5 2009 Q3, 8764, 6 2009 Q4, 5432, 7") 

I want to:

 FilterQuarter UniqueCustomerID 2009 Q1 1234 2009 Q2 1324 2009 Q3 8764 2009 Q3 1234 2009 Q4 5432 

How to do it in R? I tried the unique function, but it does not work the way I want.

+6
source share
2 answers

Long commentary within the OP becomes difficult to follow. You are looking for duplicated as @RomanLustrik pointed out . Use it for a subset of the original data.frame , like this ...

 masterdf[ ! duplicated( masterdf[ c("Quarter" , "CustomerID") ] ) , ] # Quarter CustomerID #1 2009 Q1 1234 #3 2009 Q2 1324 #4 2009 Q3 1234 #6 2009 Q3 8764 #7 2009 Q4 5432 
+10
source

Another easy way is to use SQL queries from R, check the codes below. This assumes masterdf is the name of the source file ...

 library(sqldf) sqldf("select Quarter, CustomerID from masterdf group by 1,2") 
+2
source

Source: https://habr.com/ru/post/952254/


All Articles