In the following example
x <- data.frame(code = 7:9, food = c('banana', 'apple', 'popcorn'))
y <- data.frame(food = c('banana', 'apple', 'popcorn'),
isfruit = c('fruit', 'fruit', 'not fruit'))
I would like to do x <- merge(x, y)
, but the problem is that merge()
reorders the columns so that the by
column (food) comes first. How can I prevent this and have merge(x, y)
use the same column order of x and just insert the new variable (isFruit) as the third column (i.e., "code, food, isFruit" instead of "food, code, isFruit")?
I've tried this, to no avail:
merge(x, y, sort = F)
My workaround is to do this afterward
x <- x[c(2, 1, 3)]