Reputation: 25
I want to add values looked up from a reference table to a data table, based on an index column, but I get the error: "'by' must specify a uniquely valid column". Sorry if this is a duplicate, but I haven't had any success with applying suggested answers to related questions.
In the example below, the lookup table contains a value of pH for each "BH", and I want to add this value to my main data frame "df1", where df1$Habitat == LookUp$BH.
df1 = data.frame(MySites=c(1,2,3),Habitat=c(11,12,13))
LookUp = data.frame(BH=c(8,10,11,12,13,15),pH=c(4.8,5.2,4.1,3.8,5.6,4.5))
merge(x=df1,y=LookUp,by.x='df1$Habitat',by.y='LookUp$BH',all=TRUE)
This gives: Error in fix.by(by.x, x) : 'by' must specify a uniquely valid column
This seems like a simple thing, and I must be making an obvious error. Very grateful to anyone who can point out what it is.
Upvotes: 1
Views: 5290
Reputation: 3294
You've got it right, but you're thinking too much. Remove the dataset$
in the by.x
and by.y
statements.
merge(x=df1,y=LookUp,by.x='Habitat',by.y='BH',all=TRUE)
Upvotes: 1