Nader Mehri
Nader Mehri

Reputation: 556

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]

Upvotes: 0

Views: 48

Answers (2)

Ali Fradi
Ali Fradi

Reputation: 90

names(mydata) %in% variables$variable returns a boolean vector of TRUE/FALSE depending on if column names of "mydata" dataset exists in the defined vector variables$variable . Hence, with data=mydata[,names(mydata) %in% variables$variable] "data" will have all observations (rows) of "mydata" with all columns in variables$variable

Upvotes: 1

akrun
akrun

Reputation: 887951

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

Upvotes: 2

Related Questions