[Rd] Why change data type when dropping to one-dimension?

Jason Vertrees jv at cs.dartmouth.edu
Sat May 30 00:17:28 CEST 2009


Thomas Lumley wrote:
> On Fri, 29 May 2009, Jason Vertrees wrote:
> 
>> My question is: why does the paradigm of changing the type of a 1D
>> return value to an unlisted array exist?  This introduces boundary
>> conditions where none need exist, thus making the coding harder and
>> confusing.
>>
>> For example, consider:
>>  > d = data.frame(a=rnorm(10), b=rnorm(10));
>>  > typeof(d);            # OK;
>>  > typeof(d[,1]);          # Unexpected;
>>  > typeof(d[,1,drop=F]);    # Oh, now I see.
> 
> It does make it harder for programmers, but it makes it easier for
> non-programmers.  In particular, it is convenient to be able to do
> d[1,1] to extract a number from a matrix, rather than having to
> explicitly coerce the result to stop it being a matrix.
> 
> At least the last two times this was discussed, there ended up being a
> reasonable level of agreement that if someone's life had to be made
> harder the programmers were better able to cope and that dropping
> dimensions was preferable.
> 
>     -thomas
> 
> Thomas Lumley            Assoc. Professor, Biostatistics
> tlumley at u.washington.edu    University of Washington, Seattle


Thomas,

Thanks for the quick response.  I agree that extracting a number from a
matrix/frame should result in a number not a matrix/frame.  But, why do
that for a 1D array of numbers?  In my example,
  > d[,1];
is an array, not a single number.  How does that help the novice user?

I guess I just don't like the idea that the default result is to act
unexpectedly and that a flag or boundary-conditional code is needed to
"do the right thing".

Regardless that's how it is, so I just need to learn the pitfalls for
where that occurs.

Thanks again,

-- Jason

-- 

Jason Vertrees, PhD

Dartmouth College : jv at cs.dartmouth.edu
Boston University : jasonv at bu.edu

PyMOLWiki : http://www.pymolwiki.org/



More information about the R-devel mailing list