# RE: [R] Getting subsets of a data frame

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Sun 17 Apr 2005 - 02:17:52 EST

> Because a data frame can hold different data types (even matrices) in
> different variables, one row of it can not be converted to a vector in
> general (where all elements need to be of the same type).
>
> Andy
>
>> From: Fernando Saldanha
>>
>>
>> I also noticed that
>>
>> sw[, 1, drop = TRUE] is a vector (coerces to the lowest dimension)
>>
>> but
>>
>> sw[1, , drop = TRUE] is a one-row data frame (does not convert it into
>> a list or vector)
>>
>> FS
>>
>>
>> On 4/16/05, Bill.Venables@csiro.au <Bill.Venables@csiro.au> wrote:
>>> You should look at
>>>
>>>> ?"["
>>>
>>> and look very carefully at the "drop" argument. For your example
>>>
>>>> sw[, 1]
>>>
>>> is the first component of the data frame, but
>>>
>>>> sw[, 1, drop = FALSE]
>>>
>>> is a data frame consisting of just the first component, as
>>> mathematically fastidious people would expect.
>>>
>>> This is a convention, and like most arbitrary conventions
>> it can be very
>>> useful most of the time, but some of the time it can be a very nasty
>>> trap. Caveat emptor.
>>>
>>> Bill Venables.
>>>
>>> -----Original Message-----
>>> From: r-help-bounces@stat.math.ethz.ch
>>> [mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of
>> Fernando Saldanha
>>> Sent: Saturday, 16 April 2005 1:07 PM
>>> To: Submissions to R help
>>> Subject: [R] Getting subsets of a data frame
>>>
>>>
>>> There is a list of examples of expressions using [ and [[, with the
>>> outcomes. I was puzzled by the fact that, if sw is a data
>> frame, then
>>>
>>> sw[, 1:3]
>>>
>>> is also a data frame,
>>>
>>> but
>>>
>>> sw[, 1]
>>>
>>> is just a vector.
>>>
>>> Since R has no scalars, it must be the case that 1 and 1:1
>> are the same:
>>>
>>>> 1 == 1:1
>>> [1] TRUE
>>>
>>> Then why isn't sw[,1] = sw[, 1:1] a data frame?
>>>
>>> FS
>>>
>>> ______________________________________________
>>> R-help@stat.math.ethz.ch mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> http://www.R-project.org/posting-guide.html
>>>
>>
>> ______________________________________________
>> R-help@stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> http://www.R-project.org/posting-guide.html
>>
>>
>>
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
>

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