Reputation: 997
I have two data frames DFS and DFE. DFS has the start dates at which a set of employees started a job. and DFE has the end dates at which they completed the job. The order in which the employee IDs appear in the two frames may be different.
DFS <- data.frame(ID=c('ID1','ID2','ID3'), begin=c('3/1/06 18:20', '2/1/07 15:30', '5/3/06 9:00'));
DFE <- data.frame(ID=c('ID3', 'ID2','ID1'), end=c('4/1/10 12:00', '6/1/11 14:20', '1/1/09 11:10'));
I want to create a data frame that matches the begin end end dates for each employee (e.g. output below):
DFR <-data.frame(ID=c('ID1','ID2','ID3'), begin=c('3/1/06 18:20', '2/1/07 15:30', '5/3/06 9:00'),
end=c('1/1/09 11:10', '6/1/11 14:20','4/1/10 12:00' ) );
This can be done in a loop. But I would like to know if there is an alternative method for matching and merging in R that does not involve loops. Thanks for your help.
Upvotes: 3
Views: 9268
Reputation: 42689
merge
does what you want. It will create a data frame of the joined data. Here, you want to join on the ID
column, which is specified with by
.
by
defaults to the intersection of the column names between the data frames, so if there are no other common columns, it need not be specified.
merge(DFS, DFE, by='ID')
## ID begin end
## 1 ID1 3/1/06 18:20 1/1/09 11:10
## 2 ID2 2/1/07 15:30 6/1/11 14:20
## 3 ID3 5/3/06 9:00 4/1/10 12:00
Upvotes: 7