[R] automatic coercicion

Boris Steipe boris.steipe at utoronto.ca
Sun Mar 22 03:40:15 CET 2015


See the help page for the "[" operator ...
?"["

... and use the drop parameter as in:
A[1,,drop=FALSE]

   [,1] [,2] [,3] [,4]
a1    1    4    7   10



B.

On Mar 21, 2015, at 8:10 AM, J Robertson-Burns <robertsonburns at btinternet.com> wrote:

> This is Circle 8.1.44 of 'The R Inferno'.
> 
> http://www.burns-stat.com/documents/books/the-r-inferno/
> 
> Pat
> 
> On 21/03/2015 11:04, Jean.Coursol at math.u-psud.fr wrote:
>> My question must be a trivial one.
>> 
>> There is automatic coercicion to vector when extracting only one line of a
>> matrix.
>> # example
>> A = matrix(1:12,3,4)
>> rownames(A) = c('a1','a2','a3')
>> 
>> i = 1:2
>> A[i,]
>> #    [,1] [,2] [,3] [,4]
>> # a1    1    4    7   10    matrix
>> # a2    2    5    8   11
>> 
>> i = 1
>> A[i,]
>> # [1]  1  4  7 10           vector !!!
>> 
>> # to get the rowname, it is necessary to do
>> rownames(A)[i]
>> # [1] "a1"
>> 
>> Is it possible to get a (1,4)-matrix (without testing length(i) ) ? I see
>> nothing in options()...
>> 
>> Jean Coursol
>> 
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>> 
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



More information about the R-help mailing list