Gabor Grothendieck
2002-Feb-26 01:47 UTC
[R] Matrix of Elements of Different Types (was Interfacing pre-existing C++ library from R)
--- Prof Brian D Ripley <ripley@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@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Prof Brian D Ripley
2002-Feb-26 07:26 UTC
[R] Re: Matrix of Elements of Different Types (was Interfacing pre-existing C++ library from R)
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?> dim(my.list) <- c(3,3) > my.list[,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. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Agustin Lobo
2002-Feb-26 10:06 UTC
[R] 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.matriza11 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) > midataframeX1 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")) > midataframeX1 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Gabor Grothendieck
2002-Feb-26 14:21 UTC
[R] Re: Matrix of Elements of Different Types (was Interfacing pre-existing C++ library from R)
--- Prof Brian D Ripley <ripley@stats.ox.ac.uk> wrote:>On Mon, 25 Feb 2002, Gabor Grothendieck wrote: >> Is there a way to create a matrix out of elements of different types? > >> dim(my.list) <- c(3,3) >> my.list > [,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!Actually I had tried my.list$dim <- c(3,3) but that did not seem to work. This gives rise to a number of questions since things appear to work a bit different than my previous conception of them: - what is the difference between my.list$dim<- and dim(my.list)<- ? - after creating the matrix with both numbers and my.lm elements in it I tried to convert it to a data frame. I tried data.frame(my.mat) and as.data.frame(my.mat) but using R 1.4.0 on Windows 2000 I got Error in format(unlist(x), trim = trim) : first argument must be atomic for both. Is it possible to convert my.mat to a data frame? _____________________________________________________________ _____________________________________________________________ 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@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._