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

What this line of code does using names and %in%?

I was wondering what the code below does.

data=mydata[,names(mydata) %in% variables$variable]

>Solution :

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

It subsets the columns of ‘mydata’ that are also found in the variable column in ‘variables’. It can be also written as

mydata[intersect(names(mydata), variables$variable)]

Or with dplyr

library(dplyr)
mydata %>%
    select(any_of(variables$variable))
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