[R] convert factor dataframe into numeric matrix
Prof Brian Ripley
ripley at stats.ox.ac.uk
Thu Oct 25 08:18:48 CEST 2007
On Wed, 24 Oct 2007, sun wrote:
> I took a look at these two ref cards and indeed they are helpful, but still
> they are not complete enough to include this data.matrix() function in
> particular.
But note that it is linked from ?as.matrix. Although the 'See Also'
sections of the help pages often link to things that may seem off-topic,
they are a very useful way to find cognate functionality.
>
>
> ----- Original Message -----
> From: "John Kane" <jrkrideau at yahoo.ca>
> To: "sun" <flyhyena at yahoo.com.cn>; <r-help at stat.math.ethz.ch>
> Sent: Wednesday, October 24, 2007 4:36 PM
> Subject: Re: [R] convert factor dataframe into numeric matrix
>
>
>> You might want to have a look at the two "R reference
>> card" documents under "Contributed" on CRAN.
>>
>> --- sun <flyhyena at yahoo.com.cn> wrote:
>>
>>>
>>> ----- Original Message -----
>>> From: "Prof Brian Ripley" <ripley at stats.ox.ac.uk>
>>> To: "sun" <flyhyena at yahoo.com.cn>
>>> Cc: <r-help at stat.math.ethz.ch>
>>> Sent: Wednesday, October 24, 2007 2:58 PM
>>> Subject: Re: [R] convert factor dataframe into
>>> numeric matrix
>>>
>>>
>>>> On Wed, 24 Oct 2007, sun wrote:
>>>>
>>>>> Bear me if this is a naive question.
>>>>>
>>>>> I have a dataframe, all lists inside it are
>>> factors. When I use
>>>>
>>>> Do you mean 'all columns are factors'? A data
>>> frame is a list but usually
>>>> does not contain lists.
>>>>
>>>>> as.matrix(df) to convert, I get a character
>>> matrix while my intention is
>>>>> to
>>>>> get numeric matrix. I can make the convertion
>>> iteratively by each list
>>>>> but
>>>>> I do think there is some more elegant way of
>>> doing this.
>>>>>
>>>>> please let me know if I missed somthing there.
>>>>
>>>> ?data.matrix may be what you are looking for: you
>>> gave us too little
>>>> detail to be sure.
>>>>
>>>
>>>
>>> Thanks for your helps. I did not provide data 'cause
>>> I thought this is FAQ
>>> type of question(maybe wrong impression).
>>> Actually I use algdesign generated a factorial
>>> design data frame in which:
>>>>
>>>> str(dat)
>>> 'data.frame': 2304 obs. of 6 variables:
>>> $ party : Factor w/ 3 levels "1","2","3": 1 2 3 1 2
>>> 3 1 2 3 1 ...
>>> $ dinner: Factor w/ 3 levels "1","2","3": 1 1 1 2 2
>>> 2 3 3 3 1 ...
>>> $ d1 : Factor w/ 4 levels "1","2","3","4": 1 1 1
>>> 1 1 1 1 1 1 2 ...
>>> $ p1 : Factor w/ 4 levels "1","2","3","4": 1 1 1
>>> 1 1 1 1 1 1 1 ...
>>> $ d2 : Factor w/ 4 levels "1","2","3","4": 1 1 1
>>> 1 1 1 1 1 1 1 ...
>>> $ p2 : Factor w/ 4 levels "1","2","3","4": 1 1 1
>>> 1 1 1 1 1 1 1 ...
>>>
>>> I just figured out a way fo convert it to numeric
>>> matrix using
>>>> d = as.data.frame(lapply(dat,as.numeric))
>>>
>>> but "data.matrix()" suggested by Prof. Brian and Mr.
>>> Dimitris is exactly the
>>> thing I was looking for.
>>>
>>> I am not sure if there exist some documents or
>>> online source that have
>>> documented these kind of "utility functions"
>>> categorized in somet categories
>>> like "data types", "data manipulating functions",
>>> "variable scopes", etc.,
>>> that allow a quick browsing for some functions or
>>> other information.
>>>
>>> Thanks for your kind helps.
>>>
>>> Kind regards,
>>> Sun
>>>
>>> ______________________________________________
>>> 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.
>>>
>>
>>
>>
>
>> right on your Mail page. Start today at
>> http://mrd.mail.yahoo.com/try_beta?.intl=ca
>
> ______________________________________________
> 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.
>
--
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 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list