[Rd] x <- 1:2; dim(x) <- 2? A vector or not?
Martin Maechler
maechler at stat.math.ethz.ch
Fri Jan 16 15:52:04 CET 2009
>>>>> "PatB" == Patrick Burns <pburns at pburns.seanet.com>
>>>>> on Tue, 13 Jan 2009 17:00:40 +0000 writes:
PatB> Henrik Bengtsson wrote:
>> Hi.
>>
>> On Mon, Jan 12, 2009 at 11:58 PM, Prof Brian Ripley
>> <ripley at stats.ox.ac.uk> wrote:
>>
>>> What you have is a one-dimensional array: they crop up
>>> in R most often from table() in my experience.
>>>
>>>
>>>> f <- table(rpois(100, 4)) str(f)
>>>>
>>> 'table' int [, 1:10] 2 6 18 21 13 16 13 4 3 4 - attr(*,
>>> "dimnames")=List of 1 ..$ : chr [1:10] "0" "1" "2" "3"
>>> ...
>>>
>>> and yes, f is an atmoic vector and yes, str()'s notation
>>> is confusing here but if it did [1:10] you would not
>>> know it was an array. I recall discussing this with
>>> Martin Maechler (str's author) last century, and I've
>>> just checked that R 2.0.0 did the same.
>>>
>>> The place in which one-dimensional arrays differ from
>>> normal vectors is how names are handled: notice that my
>>> example has dimnames not names, and ?names says
>>>
>>> For a one-dimensional array the 'names' attribute really
>>> is 'dimnames[[1]]'.
>>>
>>
>> Thanks for this explanation. One could then argue that
>> [1:10,] is somewhat better than [,1:10], but that is just polish.
yes. And honestly I don't remember anymore why I chose the
"[,1:n]" notation. It definitely was there already before R
came into existence, as S also has had one-dimensional arrays,
and I programmed the first version of str() in 1990.
PatB> Perhaps it could be:
PatB> [1:10(,)]
PatB> That is weird enough that it should not lead people to
PatB> believe that it is a matrix. But might prompt them a
PatB> bit in that direction.
Well, str() was always aimed a bit at experienced S (and R)
users, and I had always aimed somewhat to keep it's output
"compact". I'm quite astonished that the OP didn't know about
1D arrays in spite of the many years he's been using R.
Would a wierd solution like the above have helped?
At the moment, I'd tend to keep it "as is" if only just for
historical reminescence, but I can be convinced to change the
current "tendency" ...
Martin Maechler, ETH Zurich
PatB> Patrick Burns patrick at burns-stat.com +44 (0)20 8525
PatB> 0696 http://www.burns-stat.com (home of "The R
PatB> Inferno" and "A Guide for the Unwilling S User")
>> /Henrik
>>
>>
>>> I think these days we have enough internal glue in place
>>> that an end user would not notice the difference (but
>>> those working at C level with R objects may need to
>>> know).
>>>
>>> On Mon, 12 Jan 2009, Henrik Bengtsson wrote:
>>>
>>>
>>>> Ran into the follow intermediate case in an external
>>>> package (w/ recent R v2.8.1 patched and R v2.9.0
>>>> devel):
>>>>
>>>>
>>>>> x <- 1:2 dim(x) <- 2 dim(x)
>>>>>
>>>> [1] 2
>>>>
>>>>> x
>>>>>
>>>> [1] 1 2
>>>>
>>>>> str(x)
>>>>>
>>>> int [, 1:2] 1 2
>>>>
>>>>> nrow(x)
>>>>>
>>>> [1] 2
>>>>
>>>>> ncol(x)
>>>>>
>>>> [1] NA
>>>>
>>>>> is.vector(x)
>>>>>
>>>> [1] FALSE
>>>>
>>>>> is.matrix(x)
>>>>>
>>>> [1] FALSE
>>>>
>>>>> is.array(x)
>>>>>
>>>> [1] TRUE
>>>>
>>>>> x[1]
>>>>>
>>>> [1] 1
>>>>
>>>>> x[,1]
>>>>>
>>>> Error in x[, 1] : incorrect number of dimensions
>>>>
>>>>> x[1,]
>>>>>
>>>> Error in x[1, ] : incorrect number of dimensions
>>>>
>>>> Is str() treating single-dimension arrays incorrectly?
>>>>
>>>> What does it mean to have a single dimension this way?
>>>> Should it equal a vector? I am aware of "is.vector
>>>> returns FALSE if x has any attributes except names".
>>>>
>>>> /Henrik
>>>>
>>>> ______________________________________________
>>>> R-devel at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>>>
>>>>
>>> --
>> 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
>>>
>>> ______________________________________________
>>> R-devel at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>>
>>>
>>
> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>>
>>
______________________________________________
PatB> R-devel at r-project.org mailing list
PatB> https://stat.ethz.ch/mailman/listinfo/r-devel
More information about the R-devel
mailing list