[R] automatic coercicion

J Robertson-Burns robertsonburns at btinternet.com
Sat Mar 21 13:10:23 CET 2015


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



More information about the R-help mailing list