Re: [Rd] Bug in subsetting data frame (PR#13515)

From: Thomas Lumley <tlumley_at_u.washington.edu>
Date: Tue, 10 Feb 2009 04:42:34 -0800 (PST)


On Tue, 10 Feb 2009 xinlee883_at_stat.math.ethz.ch wrote:

>
> I discover annoying bug in subsetting data frame. Here is reproducable
> examples:

These are not bugs.

>> data.frame <- data.frame(x = runif(5), y = runif(5), row.names =
> c('a','b','c','d','e')
>
>> subset <- data.frame['x']
>> subset['a',]
> work
>
>> subset <- data.frame$x
>> subset['a',]
> not work

This can't possibly work, since you have explicitly requested that subset not be a data frame.

There has been recent discussion about whether row names should be added as names to vectors from a data frame, in which case subset['a'] would then work. It doesn't now, and that isn't a bug either.

>> subset <- data.frame['a',]
>> subset[,'x']
> work
>
>> subset <- data.frame[,'x']
>> subset['a',]
> not work

This is also not a bug. The drop= option to [ controls what happens when the subset has dimensions of length 1. If you want subset to be a data frame in this context, use

   subset <- data.frame[,'x',drop=FALSE] and then subset['a',] will do want you want.

        -thomas

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley_at_u.washington.edu	University of Washington, Seattle

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Tue 10 Feb 2009 - 11:44:56 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 10 Feb 2009 - 13:30:22 GMT.

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

list of date sections of archive