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 u
1 1 2 3 4
2 2 3 4 5
3 3 4 5 6
4 4 5 6 7
5 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 y
1 1 2
2 2 3
3 3 4
4 4 5
5 5 6

Or, much simpler, use the select argument of the subset function : you can then use 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 y
1 1 2
2 2 3
3 3 4
4 4 5
5 5 6

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

R> df[ , c("x","y")]
x y
1 1 2
2 2 3
3 3 4
4 4 5
5 5 6 R> subset(df, select=c(x,y))
x y
1 1 2
2 2 3
3 3 4
4 4 5
5 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])  }}

===============================
data(mtcars)

mtcars[mtcars[, "mpg"]>25, ]

                mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 mtcars[mtcars$mpg>25, ] mpg cyl disp hp drat wt qsec vs am gear carb
Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 subset(mtcars, mpg>25) mpg cyl disp hp drat wt qsec vs am gear carb
Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2


===============================
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的更多相关文章

  1. R Data Frame

    https://www.datamentor.io/r-programming/data-frame/ Check if a variable is a data frame or not We ca ...

  2. R语言 data.frame 大全

    A data frame is used for storing data tables. It is a list of vectors of equal length. For example, ...

  3. R语言合并data.frame

    Merging Data Adding Columns To merge two data frames (datasets) horizontally,  use the merge functio ...

  4. R vs Python:构建data.frame、读取csv与统计描述

    一.Python 数据框就是典型的关系型数据库的数据存储形式,每一行是一条记录,每一列是一个属性,最终构成表格的形式,这是数据科学家必须熟悉的最典型的数据结构. 1.构建数据框 import pand ...

  5. R语言Data Frame数据框常用操作

    Data Frame一般被翻译为数据框,感觉就像是R中的表,由行和列组成,与Matrix不同的是,每个列可以是不同的数据类型,而Matrix是必须相同的. Data Frame每一列有列名,每一行也可 ...

  6. 如何将R中的data frame对象的数据导入到DB

    在使用ARIMA模型来预测我们的销量的时候,如果保存预测版本进DB,以供后续分析呢 1. 在定义变量阶段我们定义了dfResult      <- data.frame() 这是一个data f ...

  7. 将R非时间序列的data.frame转变为时序格式

    将R非时间序列的data.frame转变为时序格式,常常会用到,尤其是股票数据处理中, 举例:dailyData包括两列数据:Date Close10/11/2013 871.9910/10/2013 ...

  8. R class of subset of matrix and data.frame

    a = matrix(     c(2, 4, 3, 1, 5, 7), # the data elements     nrow=2,              # number of rows   ...

  9. Reordering the columns in a data frame

    Problem You want to do reorder the columns in a data frame. Solution # A sample data frame data < ...

随机推荐

  1. [LeetCode] Basic Calculator & Basic Calculator II

    Basic Calculator Implement a basic calculator to evaluate a simple expression string. The expression ...

  2. SerDes、RocketIO、GTX

    1.SerDes:serdes = serial and deserial,就是组串器与解串器,也就是通用的高速IO. GTX,GTP,GTH等都是SERDES,只是速率不一样,XILINX叫其不同的 ...

  3. 什么是UML?分哪两类?

    统一建模语言(UML是 Unified Modeling Language的缩写)是用来对软件密集系统进行可视化建模的一种语言.UML为面向对象开发系统的产品进行说明.可视化.和编制文档的一种标准语言 ...

  4. [Windows Azure] Administering your Windows Azure AD tenant

    Administering your Windows Azure AD tenant 19 out of 20 rated this helpful - Rate this topic Publish ...

  5. 每日英语:China's Bigger Innovation Problem

    Last month's Third Plenum meeting of Chinese leaders seemed to signal Beijing's intention to experim ...

  6. Android NDK之一:什么是NDK?

    转:http://blog.csdn.net/xiruanliuwei/article/details/7560798 What is the NDK? The Android NDK is a to ...

  7. 域控制器修改IP操作步骤

    域控制器更改IP 在DC运维时,可能会碰上需要调整域控制器IP的情况,下面的操作就是在Windows Server 2008环境下域控制器更改IP的完整操作步骤. 1. 使用管理员帐号登录域控制器,依 ...

  8. jquery实现全选/反选功能

    <!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...

  9. 在PL/SQL中调用存储过程--oracle

    在oracle10中写好了存储过程,代码如下: CREATE OR REPLACE Procedure Proc_Insert ( sName in varchar2, sAge in int, sE ...

  10. 用adb命令组装PowerShell实用小工具——Android测试小助手

    [本文出自天外归云的博客园] 简介 APP性能测试一般对以下几个方面进行测试: 1.启动时间(可以通过本工具测试): 2.CPU的占用(可以通过本工具测试): 3.内存的占用(可以通过本工具测试): ...