Skip to content

Matrix of Elements of Different Types (was Interfacing pre-existing C++ library from R)

3 messages · Gabor Grothendieck, Brian Ripley, Agustin Lobo

#
On Mon, 25 Feb 2002, Gabor Grothendieck wrote:

            
[,1]        [,2]        [,3]
[1,] "Numeric,1" "Numeric,1" "List,12"
[2,] "Numeric,1" "Numeric,1" "List,12"
[3,] "Numeric,1" "Numeric,1" "List,12"

`R matrices are just vectors with a dimension attribute' should have given
you a clue!

The matrix() route does work in S.
#
The elements of  matrix MUST be of the same type.
You can convert a list into a matrix if and only if all the elements
of the list are of the same type. For example:
$a1
[1] 1 2 3

$a2
[1] 4 5 6

$a3
[1] 101 102 103

You can convert milista into milista.matriz:
First, to vector
a11 a12 a13 a21 a22 a23 a31 a32 a33
  1   2   3   4   5   6 101 102 103

Then to matrix (as you quote from  Prof Brian D Ripley
"> >and R matrices are just vectors with a dimension attribute."
[,1] [,2] [,3]
[1,]    1    4  101
[2,]    2    5  102
[3,]    3    6  103

BUT if milista just had one element with different length, then it
would be impossible to convert it into a matrix. 
Also, if milista had elements of different type, such as:
you would not be able, by definition, to convert it into
matrix, although you could convert it into a dataframe. Let's see:
X1 X2  X3
1  1  4 101
2  2  5 102
3  3  6 103
[1] 1 2 3
[1] TRUE
X1 X2  X3 X4
1  1  4 101  a
2  2  5 102  b
3  3  6 103  a
[1] TRUE
[1] FALSE

The data frame keeps each column with its own type.
Instead:
[,1] [,2] [,3]  [,4]
[1,] "1"  "4"  "101" "a"
[2,] "2"  "5"  "102" "b"
[3,] "3"  "6"  "103" "a"

would automatically convert all elements to the same
type (char)).

(In other words, it would seem "as if you had converted" to a matrix,
but you have done more than just changing the organization of the
information:
you have changed the information itself, as 1 != "1". Beware of that,
as R would not warn you).

So:

vector: chain of elements, all of the same type (1 dimension). Note
that:
would produce:
[1] "1" "2" "3" "a" "b" "c"
(numbers automatically converted to chars)

matrix: vector with more than 1 dimension (actually, we normally call
them matrices if they are 2D, and arrays for >3D).

dataframe: matrix which columns can be of different type (but same length)

lists: organized assemblage of elements that can be of different types,
lengths and dimensions, i.e.:
$a1
[1] 1 2 3

$a2
     [,1] [,2] [,3]
[1,]    1    5    9
[2,]    2    6   10
[3,]    3    7   11
[4,]    4    8   12

$a3
[1] "A ver si me hago un tutorial"

$a4
function(){print("patatin patatan")}


(Well, as far as I know...)

Agus

Dr. Agustin Lobo
Instituto de Ciencias de la Tierra (CSIC)
Lluis Sole Sabaris s/n
08028 Barcelona SPAIN
tel 34 93409 5410
fax 34 93411 0012
alobo at ija.csic.es
On Mon, 25 Feb 2002, Gabor Grothendieck wrote:

            
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._