Title: Is there a way to make a logical vector to see if each element of a vector exists within all of the elements of another vector?
Tags: r
Question: Basically I have two vectors from two different data frames with different lengths and I want a logical test to see if each element of the first vector is equal to the second vector (contained), the length of the logical test would be of course the same of the first vector since I'm trying to see if each element is part of the second. to then append that test and get a truth matrix to check which elements are part of both data sets.
vec1 logTest
a    T
b    F
c    T
d    T
e    F
Comment: Welcome to stack overflow. Please make your question reproducible by pasting a sample of both your datasets: use `dput(head(your_data_sample, n))` where `n` is enough data to demonstrate the problem. And include what your expected outcome should be. This makes it easier for others to test and verify solutions.

Here is the accepted answer: EDIT: If I understood correctly you want to know for every element if vec1 if it's contained in other?
library(dplyr)
vec1 <- c(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;)
other <- c(&quot;a&quot;, &quot;b&quot;)
logTest <- vec1 %in% other
gives output
r$&gt; logTest
[1]  TRUE  TRUE FALSE
Comment for this answer: Glad it worked! That's why it's always good to post what you have tried so far!
Comment for this answer: Yes! I actually tried this solution before but it kept failing because of a typo, I really need to relearn base R, thanks.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2021:00:04/06/21', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<r>', 'Title': 'Is there a way to make a logical vector to see if each element of a vector exists within all of the elements of another vector?', 'ViewCount': 86, 'id': 66964898, 'user_AboutMe': None, 'user_AccountId': 21166255.0, 'user_DisplayName': 'Roiadams', 'user_Reputation': 21.0, 'user_Views': 6.0, 'user_id': 15563503.0}}