Keep or remove data frame columns in R

Source: Internet
Author: User

You should use either indexing or the subset function. For example:

R> df <- data.frame(x=1:5, y=2:6, z=3:7, u=4:8)R> df  x y z u1 1 2 3 42 2 3 4 53 3 4 5 64 4 5 6 75 5 6 7 8

Then you can use the which function and the - operator in column indexation:

R> df[ , -which(names(df) %in% c("z","u"))]  x y1 1 22 2 33 3 44 4 55 5 6

Or, much simpler, use the select argument of the subset function:you can and the - operator directly on a vector of column names, and you can even omit the quotes around the names!

R> subset(df, select=-c(z,u))  x y1 1 22 2 33 3 44 4 55 5 6

Note that you can also select the columns you want instead of dropping the others:

R> df[ , c("x","y")]  x y1 1 22 2 33 3 44 4 55 5 6R> subset(df, select=c(x,y))  x y1 1 22 2 33 3 44 4 55 5 6

===============================

Simple R functions to keep or remove data frame columns

This function removes columns from a data frame by name:

removeCols <- function(data, cols){ return(data[,!names(data) %in% cols]) }

This function keeps columns of a data frame by name:

keepCols <- function(data, cols){
return(data[,names(data) %in% cols]) }

or just one function

colKeepRemove <- function(data, cols, remove=1){
if(remove == 1){return(data[,!names(data) %in% cols])}
else {return(data[,!names(data) %in% cols]) }}



===============================
REF:
http://stackoverflow.com/questions/4605206/drop-columns-r-data-frame
http://stackoverflow.com/questions/5234117/how-to-drop-columns-by-name-in-a-data-frame
http://ewens.caltech.edu/2011/05/17/simple-r-functions-to-keep-or-remove-data-frame-columns/

Keep or remove data frame columns in R

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.