R

Asked • 06/25/19

How to drop columns by name in a data frame?

I have a large data set and I would like to read specific columns or drop all the others. data <- read.dta("file.dta")I select the columns that I'm not interested in: var.out <- names(data)[!names(data) %in% c("iden", "name", "x_serv", "m_serv")]and than I'd like to do something like: for(i in 1:length(var.out)) { paste("data$", var.out[i], sep="") <- NULL }to drop all the unwanted columns. Is this the optimal solution?

1 Expert Answer

By:

Jay P. answered • 06/25/19

Tutor
New to Wyzant

R tutor with 10+years of R coding experience

Jay P.

make sure to insert comma "," when typing in : data[ , !names(data) %in% c("iden", "name", "x_serv", "m_serv")]
Report

06/25/19

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.