[R] Simple question regarding name of column headers

Ista Zahn izahn at psych.rochester.edu
Fri Jul 16 17:43:02 CEST 2010


Hi Addi,

On Fri, Jul 16, 2010 at 3:22 PM, Addi Wei <addiwei at gmail.com> wrote:
>
> names(miceTrainSample)
> [1] "b_double"  "KierA2"    "KierFlex"  "Q_VSA_POS" "pID50"
>
> In the above code, how do I delete "pID50" column to store the resulting
> object without indicating column "5".  The code below does the trick, but I
> wish to delete the column by specifying "-pID50" instead of "5".
>
> names(miceTrainSample)[-5]
> [1] "b_double"  "KierA2"    "KierFlex"  "Q_VSA_POS"

If I understand you correctly, than this code will not do the trick.
All it does is print the column names minus pID50. It does nothing to
miceTrainSample.

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"]

Hope it helps,
Ista
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Simple-question-regarding-name-of-column-headers-tp2291534p2291534.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org



More information about the R-help mailing list