1

sorry for the noob question but I really don't know where to search for the procedure I must do. My problem is that I need to find all matches of one column into another database to get the value of another column. Some sort of a merge but in this case I have several repeated values in the right database, so I need to get all matches.

To be clearer: suppose that I have this kind of data frame:

df<-data.frame(CustomerId=c("a","b","c","h"))

   CustomerId
1          a
2          b
3          c
4          h

and my other dataframe would be something like:

df2<-data.frame(CustomerID=c("a","b","b","b","a","d","c"),code=c(1,2,2,3,2,4,4))
  CustomerID code
1          a    1
2          b    2
3          b    2
4          b    3
5          a    2
6          d    4
7          c    4

I need to "merge" this two dataframes so that I can get all the codes for every one of the customersIDs. I would need something like this:

  CustomerId codes
1          a   1,2
2          b   2,3
3          c     4
4          h    NA

The problems I found so far are:

  • I have my key repeated several times on the crossing dataframe
  • I could use a loop but my database is so large that I need to avoid inefficiencies
  • the amount of times that the customerId can be repeated on the second dataframe is variable
  • It could happen that one of the customerId repeat several times but only have one code so I would need only one code, not all of them
  • If a CustomerId is not found on the second database I would need a NA value

Thanks for your help guys, hope you can help me

1
  • I think this should work left_join(df,unique(df2)) %>% group_by(CustomerId) %>% summarise(codes=paste(code, collapse = ",")) but how big is your data? BTW, it works if you have exactly the same names on both dataframes, R is case sensitive (otherwise specify the by clause)
    – animalito
    Mar 18, 2015 at 4:29

2 Answers 2

2

Here's another quick solution using data.table binary join

library(data.table)
Res <- setDT(df2)[, .(codes = toString(unique(code))), key = CustomerID]
Res[df]
#    CustomerID codes
# 1:          a  1, 2
# 2:          b  2, 3
# 3:          c     4
# 4:          h    NA
1
  • 1
    Nice use of toString
    – akrun
    Mar 18, 2015 at 17:55
1

A possible solution using data.table:

library(data.table)

#Convert data.frames to data.tables
dt = data.table(df,key="CustomerId")
dt2 = data.table(df2, key ="CustomerID")

#rename to account for case sensitivity 
setnames(dt2,"CustomerID","CustomerId")

#merge files retaining all values even those that has no matching key
dt3 = merge(dt,dt2,all=T)

#list unique codes split by "CustomerId"
dt3[,list(code=list(unique(code))),by="CustomerId"]

If one feels uncomfortable working with data.tables beyond this point any data.table object might easily be reconverted to data.frameas simple as: df = data.frame(dt).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.