R: how to check if there are multiple names with the given id in a dataframe

540 views Asked by At

I have a R data like the following:

id = c(1,1,1,2,2,4,4,3,3,3)
buyernames = c("ann","ann","bo","celine","celine","mary","lily","john","john","john")
data = cbind(id,buyernames)
#id  buyernames
#[1,] "1" "ann"     
#[2,] "1" "ann"     
#[3,] "1" "bo"      
#[4,] "2" "celine"  
#[5,] "2" "celine"  
#[6,] "4" "mary"    
#[7,] "4" "lily"    
#[8,] "3" "john"    
#[9,] "3" "john"    
#[10,] "3" "john"    

ID is numeric. What I want to know is if there are ids that are associated with different names. If so, what are the ids and names.

desired output - a data like the following:

   id   buyernames
    1    ann
    1    bo
    4    mary
    4    lily

Any insight would be greatly appreciated.

1

There are 1 answers

0
K. A. Buhr On BEST ANSWER

The unique function can eliminate duplicate rows, and the duplicated function can be used to identify duplicate ids after duplicate rows are removed. So, the following should work:

id = c(1,1,1,2,2,4,4,3,3,3)
buyernames = c("ann","ann","bo","celine","celine","mary","lily","john","john","john")
data = cbind(id,buyernames)

D <- unique(data.frame(data))           # get unique rows
dupids <- with(D, id[duplicated(id)])   # duplicate ids
subset(D, id %in% dupids)               # rows for duplicate ids