merge() increasing the number of rows in R

Viewed 40

I have two data sets with common column by the name of "Id", both the data sets have 22099 rows but the number of columns are different. When I use merge() the rows are increased to 15,393,213. Why is this happening. the code is as follows

calories_merged_data <- merge(hourlyCalories_merged, hourlyIntensities_merged, by=c('Id'))

Example of hourlyCalories_merged

Id ActivityHour Calories
123 4/12/2016 12:00:00 AM 81
456 5/12/2016 06:00:00 AM 90

Example of hourlyIntensities_merged

Id Activityhour TotalIntensity AverageIntensity
123 4/12/2016 12:00:00 AM 20 0.33333
456 5/12/2016 06:00:00 AM 08 0.13333

The data is from https://www.kaggle.com/arashnic/fitbit

1 Answers

This happens because there are duplicates in the ID column, so if there are n records with the same ID, each will be merged giving n^2 records in the merged dataset. You probably need to merge on a combination of variables that is unique to each record.

Related