matching all values in two dataframe columns

EventID <- c(1,2,3,4,5)
my_participant_ref <- c(1,2,6,1,3)
event_participant_ref <- c(1,9,3,2,3)

mydf <- data.frame(EventID, my_participant_ref, event_participant_ref)

I haven't been able to find the right function and/or syntax - if there is one - to enable me to do the equivalent of

mydf$my_participant_ref %in% mydf$participant_ref

Using that doesn't give the result I need - ie 'TRUE' for EventIDs 1 and 5 - I think that's because one side of %in% needs to be a character vector so it does not work with two dataframe columns?

Is it a type? Because mydf$participant_ref does not exist.

Assuming it means mydf$event_participant_ref , here are two solutions:

# soln 1
with(data = mydf, expr = {my_participant_ref %in% event_participant_ref})

# soln 2
library(magrittr)
mydf %$% `%in%`(my_participant_ref, event_participant_ref)

But of course it doesn't explain this expected result:

So, please explain how are you getting this result, because I'm getting TRUE for 2 and 4 as well. But I should mention that certainly `%in%` is not only for character vectors.


[In reply to post below]

This is not at all the use case of %in%.

It checks whether the LHS (or each element of LHS) belong to RHS or not. But your problem is not that. You just want to check whether they match or not.

Simply use ==.

with(mydf, my_participant_ref == event_participant_ref)
1 Like

Hello

many thanks for response. Apologies for my typo, " event_participant_ref`" is the correct name.

I may have explained this badly, but in my data.frame, the only two eventIDs where event_participant_ref and my_participant_ref match are eventID=1 and eventID=5. So I am expecting to see:
TRUE FALSE FALSE FALSE TRUE

For:

mydf$my_participant_ref %in% mydf$event_participant_ref

the result is:
TRUE TRUE FALSE TRUE TRUE

mydf %$% `%in%`(event_participant_ref, my_participant_ref)

gives
TRUE FALSE TRUE TRUE TRUE

I expect I am misunderstanding how %in% operates, but I can't figure out how. Or I am just using the wrong function.

Oh yes of course, thank you, %in% looks for a value anywhere in my_participant_ref that matches a value anywhere in event_participant_ref rather than just comparing values on the same row.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.