Matrix of Elements of Different Types (was Interfacing pre-existing C++ library from R)
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:
milista <- list(a1=1:3, a2=4:6,a3=101:103)
milista
$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
milista.matriz
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."
dim(milista.matriz) <- c(3,3) milista.matriz
[,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:
milista <- list(a1=1:3, a2=4:6,a3=101:103,a4=c("a","b","c"))
you would not be able, by definition, to convert it into matrix, although you could convert it into a dataframe. Let's see:
midataframe <- data.frame(milista.matriz) midataframe
X1 X2 X3 1 1 4 101 2 2 5 102 3 3 6 103
midataframe$X1
[1] 1 2 3
is.numeric(midataframe$X1)
[1] TRUE
midataframe <- cbind(midataframe,X4=c("a","b","a"))
midataframe
X1 X2 X3 X4 1 1 4 101 a 2 2 5 102 b 3 3 6 103 a
is.numeric(midataframe$X1)
[1] TRUE
is.numeric(midataframe$X4)
[1] FALSE The data frame keeps each column with its own type. Instead:
cbind(milista.matriz,c("a","b","a"))
[,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:
mivector <- c(1,2,3,"a","b","c")
would produce:
mivector
[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.:
milista
$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:
--- Prof Brian D Ripley <ripley at stats.ox.ac.uk> wrote:
A matrix list? R lists are just vectors with elements of different types, and R matrices are just vectors with a dimension attribute.
When I saw the above I tried to create a matrix from a list but could not get it to work: my.lm <- lm( rnorm(10) ~ I(1:10) ) my.list <- list(1, 2, 3, 4, 5, 6, my.lm, my.lm, my.lm) my.mat <- matrix( my.list, nrow=3 ) gives the following error in R 1.4.0 on Windows 2000: Error in matrix(my.list, nrow = 3) : Unimplemented feature in copyVector Is there a way to create a matrix out of elements of different types? Thanks.
_____________________________________________________________ _____________________________________________________________ You deserve a better email address! Get personalized email @yourname or @yourcompany from Everyone.net --> http://www.everyone.net?tag -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._