Hi, and my apologies for the following very naive question : I would like to read a column of numbers in R and plot a histogram. eg : x<-read.table("txSTART"); y<-as.numeric(x); and I do obtain the error : Error: (list) object cannot be coerced to type 'double'. Please could you let me know the way to fix it. thanks, bogdan <r-help@r-project.org> [[alternative HTML version deleted]]
Hi, On Aug 19, 2009, at 4:59 PM, Bogdan Tanasa wrote:> Hi, and my apologies for the following very naive question : I would > like to > read a column of numbers in R and plot a histogram. > > eg : > > x<-read.table("txSTART"); > y<-as.numeric(x); > > and I do obtain the error : Error: (list) object cannot be coerced > to type > 'double'. Please could you let me know the way to fix it.Yeah, you can't do that. What does x look like? Can you show us the result of: R> head(x) Assuming it's just a single column, you can access the numbers in the first column, like so R> x[,1] You can use that to plot a histogram of the numbers in the first column: R> plot(hist(x[,1])) -steve -- Steve Lianoglou Graduate Student: Computational Systems Biology | Memorial Sloan-Kettering Cancer Center | Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact
read.table produces a data.frame (a special list) even when you have only one column. Try y<-as.numeric(x[[1]]) On Wed, Aug 19, 2009 at 3:59 PM, Bogdan Tanasa <tanasa@gmail.com> wrote:> Hi, and my apologies for the following very naive question : I would like > to > read a column of numbers in R and plot a histogram. > > eg : > > x<-read.table("txSTART"); > y<-as.numeric(x); > > and I do obtain the error : Error: (list) object cannot be coerced to type > 'double'. Please could you let me know the way to fix it. > > thanks, > > bogdan > <r-help@r-project.org> > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >-- Jun Shen PhD PK/PD Scientist BioPharma Services Millipore Corporation 15 Research Park Dr. St Charles, MO 63304 Direct: 636-720-1589 [[alternative HTML version deleted]]
Bogdan: I believe your query demonstrates the hazards of jumping into R without first making an effort to read at least some of the supporting docs -- in this case, I think, "An Intro to R" would have sufficed. R is not a video game nor even Excel. It is very powerful and quite sophisticated software for doing some pretty hairy stuff, implemented in its "raw" form as an interactive programming language (apologies if this is not the appropriate technical term). So, as I think you found out, it can make some pretty daunting demands on a beginner even for simple things. If you really need it's power and flexibility, once you've climbed the learning curve, it's an extraordinarily productive tool. If you are just a casual reader who doesn't need all that complex functionality, it's probably not worth the effort or frustration. Anyway, the answer to your question -- which may require you to read the docs to understand -- is that in R a "table" from read.table() is actually a data frame, which is also a special kind of list. As the error message says, as.numeric() cannot coerce recursive objects (which is what a list is) to simple numeric vectors. Bert Gunter Genentech Nonclinical Biostatisics -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Bogdan Tanasa Sent: Wednesday, August 19, 2009 1:59 PM To: r-help at r-project.org Subject: [R] a naive question Hi, and my apologies for the following very naive question : I would like to read a column of numbers in R and plot a histogram. eg : x<-read.table("txSTART"); y<-as.numeric(x); and I do obtain the error : Error: (list) object cannot be coerced to type 'double'. Please could you let me know the way to fix it. thanks, bogdan <r-help at r-project.org> [[alternative HTML version deleted]] ______________________________________________ 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.