[R] Dropping columns from data frame

Sarah Goslee sarah.goslee at gmail.com
Fri Jan 6 18:13:14 CET 2012


On Fri, Jan 6, 2012 at 10:00 AM, Mike Harwood <harwood262 at gmail.com> wrote:
> How does R do it, and should I ever be worried?

You should be worried, but not about that.

>  I always remove
> columns by index, and it works exactly as I would naively expect - but
> HOW?  The second illustration, which deletes non contiguous columns,
> represents what I do all the time and have some trepidation about
> because I don't know the mechanics (e.g. why doesn't the column
> formerly-known-as-4 become 3 after column 1 is dropped: doesn't vector
> removal from a df/list invoke a loop in C?).

The R programmers, and the S programmers before them, were smart
enough to make this a simultaneous rather than sequential operation.
If you really need the details, the source is out there.

> Can I delete a named
> list of columns, which are examples 4 and 5 and which generate the
> "unary error' mesages, without resorting to "orig.df$num1.10 <- NULL"?

head(orig.df[, !colnames(orig.df) %in% c('num1.10', 'lc1.10', 'uc1.10')])

Sarah

> Thanks!
>
> orig.df <- data.frame(cbind(
>        1:10
>        ,11:20
>        ,letters[1:10]
>        ,letters[11:20]
>        ,LETTERS[1:10]
>        ,LETTERS[11:20]
>        ))
> names(orig.df) <- c(
>        'num1.10'
>        ,'num11.20'
>        ,'lc1.10'
>        ,'lc11.20'
>        ,'uc1.10'
>        ,'uc11.20'
>        )
> # Illustration 1: contiguous columns at beginning of data frame
> head(orig.df[,-c(1:3)])
>
> # Illustration 2: non-contiguous columns
> head(orig.df[,-c(1,3,5)])
>
> # Illustration 3: contiguous columns at end of data frame
> head(orig.df[,-c(4:6)])         ## as expected
>
> # Illustrations 4-5: unary errors
> head(orig.df[,-c(as.list('num1.10', 'lc1.10', 'uc1.10'))])
> head(orig.df[,-c('num1.10', 'lc1.10', 'uc1.10')])
>
>
> Mike
>

-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list