Re: [R] drop

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Thu 28 Sep 2006 - 17:33:57 GMT

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@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@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

______________________________________________
R-help@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.
Received on Fri Sep 29 03:36:39 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 28 Sep 2006 - 18:30:10 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.