2

I would like to extract a selection of rows from a data frame based on multiple identifying variables contained in another data frame. Consider the following illustrative data set:

df <- data.frame(id=c(1,2,2,3,4,4,4,4,5), ref=c("A","B","C","D","E","F","F","G","H"), amount=c(10,15,20,25,30,35,-35,40,45))
required <- data.frame(id=c(2,3,4,4), ref=c("B","D","E","F"))

I would like the output in a data frame with id, ref and amount as follows:

id ref amount
2 B 15
3 D 25
4 E 30
4 F 35
4 F -35

Note in particular that id 4 and ref F have two matches from the df with amounts 35 and -35.

1 Answer 1

1

You want to merge:

merge(df, required)
##    id ref amount
##  1  2   B     15
##  2  3   D     25
##  3  4   E     30
##  4  4   F     35
##  5  4   F    -35
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.