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 :

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))

Leave a Reply