Trying to implement an answer to the question posed here , but can't see where I'm going wrong.
I am trying to extract from one dataframe observations listed in another dataframe using a common field. The question cited was not exactly the same but an answer suggested using "setdiff" for the related question, which seems to fit my need.
Here's the example I set up to try it:
# orginal dataframe
origdf <- data.frame(apple = c(111, 2, 4, "fox"),
orange=c( 222, 11, 12, 14),
pear=c( "one", "two", 10, 11),
peach=c("which", "way", "to", "go"),
banana=c(333, 22, 23, 24),
grape=c(77, 78, 79, 80))
origdf
# a separate process produces a dataframe with observations to be extracted from the original dataframe
extract <- origdf[which(origdf$apple == 111 |
origdf$apple == "fox"), ]
extract
test <- origdf[setdiff(origdf$apple, extract$apple)]
test
# the above returns an error that "undefined columns selected", but the following works...
origdf$apple
extract$apple
Why am I having this problem?