[R] Simple question regarding name of column headers

Erik Iverson eriki at ccbr.umn.edu
Fri Jul 16 18:09:24 CEST 2010


> Anyway, I have often wished that something like
> 
> new.mt.sample <- miceTrainSample[, -"pID50"]
> 
> would return miceTrainSample without the pID50 column. Here are three
> alternative ways to do it.
> 
> # Method 1: Assign NULL to the column
> new.mt.sample <- miceTrainsSample
> new.mt.sample$pID50 <- NULL
> 
> # Method 2: Use which()
> new.mt.sample <- miceTrainSample[, - which(names(miceTrainSample == "pID50")]
> 
> # Method 3: use %in% (the one I usually use)
> new.mt.sample <- miceTrainSample[, ! names(miceTrainSample) %in% "pID50"]

As a variation on Method 1...

df <- data.frame(a = 1:10, b = 2:11, c = 3:12)
transform(df, a = NULL)



More information about the R-help mailing list