Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R function to check if a vector is a subset of another

I need to check if values_saved_dice is a subset from x, for example

values_saved_dice <- c(2,2,2)
x <- c(6,3,2,2,5)

I tried the following function, expecting it should return FALSE.

all(is.element(value_saved_dices, x)

But it returns TRUE, when apparently it should be FALSE – because the number "2" appeared 3 times in value_saved_dices and x only has "2" twice.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Would appreciate any help, thanks!

>Solution :

We may paste the vector into string and use grepl to check if the substring is found or not

grepl(paste(values_saved_dice, collapse = ""), paste(x, collapse = ""))
[1] FALSE
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading