Skip to content
Back to formatted view

Raw Message

Message-ID: <4B5C470D.5070804@ucalgary.ca>
Date: 2010-01-24T13:11:41Z
From: Peter Ehlers
Subject: Is there a quicker way to drop a data frame column than setting it to NULL?
In-Reply-To: <1264334330177-1288617.post@n4.nabble.com>

Dimitri Shvorob wrote:
> If I want to drop columns x, y, z from dataframe df, is there a better
> alternative to
> 
> df$x = NULL
> df$y = NULL
> df$z = NULL
> 
> There are sufficiently many columns remaining to make 
> 
> df = subset(df, select = c(a,b,c,d[etc]))
> 
> cumbersome.

You can use subset with the select argument specifying
which variables to omit:

df = subset(df, select = !names(df) %in% c('x', 'y', 'z'))

or just

df[!names(df) %in% c('x', 'y', 'z')]

  -Peter Ehlers

> 
> Thank you.

-- 
Peter Ehlers
University of Calgary