Given an arbitrary data frame, it is easy to exclude a column given its index: df[,-2]. How to do the same thing given the column name? A naive attempt df[,-"name"] did not work :)
Zeljko Vrba wrote:> Given an arbitrary data frame, it is easy to exclude a column given its index: > df[,-2]. How to do the same thing given the column name? A naive attempt > df[,-"name"] did not work :) > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >Hi, This piece of code does the trick. Most important is the which() command: df = data.frame(a = runif(10), b = runif(10)) df[,-which(names(df) == "a")] cheers, Paul -- Drs. Paul Hiemstra Department of Physical Geography Faculty of Geosciences University of Utrecht Heidelberglaan 2 P.O. Box 80.115 3508 TC Utrecht Phone: +3130 274 3113 Mon-Tue Phone: +3130 253 5773 Wed-Fri http://intamap.geo.uu.nl/~paul
Hope this helps:> df <- data.frame(matrix(1:10,2)) > dfX1 X2 X3 X4 X5 1 1 3 5 7 9 2 2 4 6 8 10> df[,-2]X1 X3 X4 X5 1 1 5 7 9 2 2 6 8 10> df[,-which(names(df)=="X2")]X1 X3 X4 X5 1 1 5 7 9 2 2 6 8 10 On Wed, May 27, 2009 at 6:37 PM, Zeljko Vrba <zvrba at ifi.uio.no> wrote:> Given an arbitrary data frame, it is easy to exclude a column given its index: > df[,-2]. ?How to do the same thing given the column name? ?A naive attempt > df[,-"name"] did not work :)
On Wed, May 27, 2009 at 12:52:41PM +0200, Paul Hiemstra wrote:> > This piece of code does the trick. Most important is the which() command: > > df = data.frame(a = runif(10), b = runif(10)) > df[,-which(names(df) == "a")] >Thanks to you and Linlin. It did not occur to me to use which(); I thought that there would be a shorter way to accomplish this since names are first-class indices for data frames and arrays. (Or are they? What happens under the hood when I write df[,"a"]?)
You can try this: DF[,"columnName"] <- NULL On Wed, May 27, 2009 at 7:37 AM, Zeljko Vrba <zvrba@ifi.uio.no> wrote:> Given an arbitrary data frame, it is easy to exclude a column given its > index: > df[,-2]. How to do the same thing given the column name? A naive attempt > df[,-"name"] did not work :) > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
On Wed, May 27, 2009 at 6:37 AM, Zeljko Vrba <zvrba@ifi.uio.no> wrote:> Given an arbitrary data frame, it is easy to exclude a column given its > index: > df[,-2]. How to do the same thing given the column name? A naive attempt > df[,-"name"] did not work :) >Various ways: Boolean index vector: df[ , names(df) != "name" ] List of wanted column names: df[ , setdiff(names(df), "name") ] Negated list of unwanted column indexes: df[ , -match("name",names(df)) ] df[ , -which(names(df) == "name") ] The special 'subset' hack for column names; beware, I think this is the only place in R where you can negate a column name. subset(df , select = -a ) Hope this helps, -s [[alternative HTML version deleted]]