[R] automatic coercicion

Jean.Coursol at math.u-psud.fr Jean.Coursol at math.u-psud.fr
Sat Mar 21 12:04:00 CET 2015


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



More information about the R-help mailing list