Reputation: 3
I have the following two dataframes: all_cards, which has 1,334 observations of 32 variables, and contacts, which has 1,321 observations of 12 variables. Both dataframes contain a column called id, that has the same id numbers, though not in the same order (all_cards has 13 more id numbers than contacts).
I would like to add two of the variables (columns) from all_cards to contacts.
I attempted this using the following code;
contacts2 <- merge(x = contacts, y = all_cards[,c("idList", "idLabels")], by = "id")
and received the following error;
Error in fix.by(by.y, y) : 'by' must specify a uniquely valid column
I get the same error when I specify by.x = "id" and by.y = "id". I have checked and both dataframes only have one column called "id", and both columns are of class "character". I am at a loss as to why this merge function does not work.
My desired outcome would be to have the dataframe, contacts2, with 1,321 observations of 14 variables, the last two being idList and idLabels.
I am using RStudio version 1.1.456 on a Mac running macOS Sierra (version 10.12.6). Any help would be greatly appreciated.
Upvotes: 0
Views: 393
Reputation: 582
The by
column has to exist in both data.frame
, therefore the column id
has to be also contained in the y data.frame
:
contacts2 <- merge(contacts, all_cards[, c("id", "idList", "idLabels")], by = "id")
Upvotes: 1