Bear me if this is a naive question. I have a dataframe, all lists inside it are factors. When I use 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. Thanks in advance. Sun
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 useDo 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. -- 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
Dimitris Rizopoulos
2007-Oct-24 12:59 UTC
[R] convert factor dataframe into numeric matrix
look at ?data.matrix(). Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "sun" <flyhyena at yahoo.com.cn> To: <r-help at stat.math.ethz.ch> Sent: Wednesday, October 24, 2007 2:51 PM Subject: [R] convert factor dataframe into numeric matrix> Bear me if this is a naive question. > > I have a dataframe, all lists inside it are factors. When I use > 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. > > Thanks in advance. > > 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Hi r-help-bounces at r-project.org napsal dne 24.10.2007 14:51:10:> Bear me if this is a naive question. > > I have a dataframe, all lists inside it are factors. When I use > as.matrix(df) to convert, I get a character matrix while my intention isto> get numeric matrix. I can make the convertion iteratively by each listbut> I do think there is some more elegant way of doing this. > > please let me know if I missed somthing there.You missed to provide several information, mainly some example. 1. How did you get nonumeric data frame? If it is by transfer from Excel or something like that, you definitelly shall look at raw data as there is some mismatch. R tries to keep numeric as numeric but converts character to factor. 2. Are the values numeric? I presume that yes. So you can use e.g. sapply sapply(dat, function(x) as.numeric(as.character(x))) Regards Petr> > Thanks in advance. > > Sun > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.