[R] drop

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Sep 28 19:33:57 CEST 2006


On Thu, 28 Sep 2006, Hans-Peter wrote:

> Hello,
>
> What's the best/recommended way to drop the dimension of a data.frame?
> Probably "test[,,]" but I am not sure. I would prefer an explicit
> function, but drop doesn't work with frames and I didn't find
> something in the helpfiles/mailarchive.

The recommended way is not to do it.  If as it appears you want to extract 
a single column, do so explicitly by e.g.

if(length(test) == 1) test[[1]] else test

Your as.matrix versions will not work with general columns.

>
> Example:
>
> test <- data.frame( x = 1:7 )
>
> str( drop( test ) )                     # still a data.frame
>
> str( test[,, drop = TRUE] )             # ok
> str( test[,,] )                         # ok <== IS THIS THE WAY TO DO IT?
> str( as.integer( as.matrix( test ) ) )  # ok
>
> str( drop( as.matrix( test ) ) )        # ~ok, named integer
> test2 <- test
> attributes( test2 ) <- NULL
> str( drop( as.matrix( test2 ) ) )       # ok
>
>
> Thanks and best regards,
> Hans-Peter
>
> PS: how could I lookup the code for subsetting a data.frame. I suppose
> that it is in "data.frame.["
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list