Andrew Yee
2007-May-16 12:10 UTC
[R] more woes trying to convert a data.frame to a numerical matrix
I have the following csv file: name,x,y,z category,delta,gamma,epsilon a,1,2,3 b,4,5,6 c,7,8,9 I'd like to create a numeric matrix of just the numbers in this csv dataset. I've tried the following program: sample.data <- read.csv("sample.csv") numerical.data <- as.matrix(sample.data[-1,-1]) However, print(numerical.data) returns what appears to be a matrix of characters: x y z 2 "1" "2" "3" 3 "4" "5" "6" 4 "7" "8" "9" How do I force it to be numbers rather than characters? Thanks, Andrew [[alternative HTML version deleted]]
ONKELINX, Thierry
2007-May-16 12:21 UTC
[R] more woes trying to convert a data.frame to a numerical matrix
?as.numeric ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Reseach Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx op inbo.be www.inbo.be Do not put your faith in what statistics say until you have carefully considered what they do not say. ~William W. Watt A statistical analysis, properly conducted, is a delicate dissection of uncertainties, a surgery of suppositions. ~M.J.Moroney> -----Oorspronkelijk bericht----- > Van: r-help-bounces op stat.math.ethz.ch > [mailto:r-help-bounces op stat.math.ethz.ch] Namens Andrew Yee > Verzonden: woensdag 16 mei 2007 14:11 > Aan: r-help op stat.math.ethz.ch > Onderwerp: [R] more woes trying to convert a data.frame to a > numerical matrix > > I have the following csv file: > > name,x,y,z > category,delta,gamma,epsilon > a,1,2,3 > b,4,5,6 > c,7,8,9 > > I'd like to create a numeric matrix of just the numbers in > this csv dataset. > > I've tried the following program: > > sample.data <- read.csv("sample.csv") > numerical.data <- as.matrix(sample.data[-1,-1]) > > However, print(numerical.data) returns what appears to be a matrix of > characters: > > x y z > 2 "1" "2" "3" > 3 "4" "5" "6" > 4 "7" "8" "9" > > How do I force it to be numbers rather than characters? > > Thanks, > Andrew > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help op stat.math.ethz.ch 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. >
Marc Schwartz
2007-May-16 12:36 UTC
[R] more woes trying to convert a data.frame to a numerical matrix
On Wed, 2007-05-16 at 08:10 -0400, Andrew Yee wrote:> I have the following csv file: > > name,x,y,z > category,delta,gamma,epsilon > a,1,2,3 > b,4,5,6 > c,7,8,9 > > I'd like to create a numeric matrix of just the numbers in this csv dataset. > > I've tried the following program: > > sample.data <- read.csv("sample.csv") > numerical.data <- as.matrix(sample.data[-1,-1]) > > However, print(numerical.data) returns what appears to be a matrix of > characters: > > x y z > 2 "1" "2" "3" > 3 "4" "5" "6" > 4 "7" "8" "9" > > How do I force it to be numbers rather than characters? > > Thanks, > AndrewThe problem is that you have two rows which contain alpha entries. The first row is treated as the header, but the second row is treated as actual data, thus overriding the numeric values in the subsequent rows. You could use: as.numeric(as.matrix(sample.data[-1, -1])) to coerce the matrix to numeric, or if you don't need the alpha entries, you could modify the read.csv() call to something like: read.csv("sample.csv", header = FALSE, skip = 2, row.names = 1, col.names = c("name", "x", "y", "z") This will skip the first two rows, set the first column to the row names and give you a data frame with numeric columns, which in most cases can be treated as a numeric matrix and/or you could explicitly coerce it to one. HTH, Marc Schwartz
Dimitris Rizopoulos
2007-May-16 12:39 UTC
[R] more woes trying to convert a data.frame to a numerical matrix
have a look at: ?as.numeric() and ?data.matrix(). I hope it helps. 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: "Andrew Yee" <andrewjyee at gmail.com> To: <r-help at stat.math.ethz.ch> Sent: Wednesday, May 16, 2007 2:10 PM Subject: [R] more woes trying to convert a data.frame to a numerical matrix>I have the following csv file: > > name,x,y,z > category,delta,gamma,epsilon > a,1,2,3 > b,4,5,6 > c,7,8,9 > > I'd like to create a numeric matrix of just the numbers in this csv > dataset. > > I've tried the following program: > > sample.data <- read.csv("sample.csv") > numerical.data <- as.matrix(sample.data[-1,-1]) > > However, print(numerical.data) returns what appears to be a matrix > of > characters: > > x y z > 2 "1" "2" "3" > 3 "4" "5" "6" > 4 "7" "8" "9" > > How do I force it to be numbers rather than characters? > > Thanks, > Andrew > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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