[R] transform a table into a matrix

Martin Maechler maechler at stat.math.ethz.ch
Tue Jun 24 18:36:19 CEST 2008


>>>>> "JohnF" == John Fox <jfox at mcmaster.ca>
>>>>>     on Tue, 24 Jun 2008 08:10:43 -0400 writes:

    JohnF> Dear Maria, On 23-Jun-08, at 11:40 PM, Maria
    JohnF> Gavilanez wrote:

    >> Hi.
    >> 
    >> is there a way to transform a table that has numeric and
    >> categorical variables into a matrix?

    JohnF> No, because all of the elements of a matrix must be
    JohnF> of the same mode, but a dataframe is a data structure
    JohnF> that can have heterogeneous columns. It's unclear
    JohnF> from your question where the "table" is located -- in
    JohnF> a file perhaps? If so, you can use read.table() to
    JohnF> read the data into a dataframe.

Yes, and then possibly use  data.matrix(.) to produce a numeric
matrix where the "categorical variables" have become numeric
(via their internal codes).

Martin Maechler, ETH Zurich

    JohnF> I hope this helps, John

    JohnF> -------------------------------------------- John
    JohnF> Fox, Professor Department of Sociology McMaster
    JohnF> University Hamilton ON Canada L8S 4M4 web:
    JohnF> socserv.mcmaster.ca/jfox

    >> 
    >> 
    >> thanks
    >> 
    >> 
    >> Maria Mercedes Gavilanez Department of Biological
    >> Sciences 107 Life Sciences Building Louisiana State
    >> University Baton Rouge, LA 70803 (225)578-4284
    >> 
    >> ______________________________________________
    >> R-help at r-project.org mailing list
    >> 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.

    JohnF> ______________________________________________
    JohnF> R-help at r-project.org mailing list
    JohnF> https://stat.ethz.ch/mailman/listinfo/r-help PLEASE
    JohnF> do read the posting guide
    JohnF> http://www.R-project.org/posting-guide.html and
    JohnF> provide commented, minimal, self-contained,
    JohnF> reproducible code.



More information about the R-help mailing list