R function to merge data