I am reading a list of numbers from my clipboard, and have been successful, except I can't make a histogram as R doesn't recognize my variable as numeric. I know I need to use "as.is", but the specifics escape me. I have used x<-read.table("clipboard", header=F) to import from a txt file. How do make this numeric? Thanks, J.
If read.table() is not recognizing the data as numeric, there's probably something in the content that confused read.table(). You can try using x <- scan("clipboard") instead and see if and how it chokes. Andy> From: Janet Gannon > > I am reading a list of numbers from my clipboard, and have been > successful, except I can't make a histogram as R doesn't recognize my > variable as numeric. I know I need to use "as.is", but the specifics > escape me. > > I have used x<-read.table("clipboard", header=F) to import from a txt > file. How do make this numeric? Thanks, J. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments,...{{dropped}}
Janet Gannon wrote:> I am reading a list of numbers from my clipboard, and have been > successful, except I can't make a histogram as R doesn't recognize my > variable as numeric. I know I need to use "as.is", but the specifics > escape me. > I have used x<-read.table("clipboard", header=F) to import from a txt > file. How do make this numeric? Thanks, J. >Please (re-)read ?read.table. Specifically, the "as.is" and "colClasses" arguments. -sundar
Look at the help for: ?as.numeric HTH, Andy> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Janet Gannon > Sent: Monday, March 15, 2004 7:04 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Simple numeric "as.is" question > > > I am reading a list of numbers from my clipboard, and have been > successful, except I can't make a histogram as R doesn't recognize my > variable as numeric. I know I need to use "as.is", but the specifics > escape me. > > I have used x<-read.table("clipboard", header=F) to import from a txt > file. How do make this numeric? Thanks, J. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo> /r-help > PLEASE > do read the posting guide! > http://www.R-project.org/posting-guide.html > >
On Mon, Mar 15, 2004 at 09:04:05AM -0500, Janet Gannon wrote:> I am reading a list of numbers from my clipboard, and have been > successful, except I can't make a histogram as R doesn't recognize my > variable as numeric. I know I need to use "as.is", but the specifics > escape me. > > I have used x<-read.table("clipboard", header=F) to import from a txt > file. How do make this numeric? Thanks, J.x <- read.table("clipboard", header=F) x <- as.data.frame(lapply(x, as.numeric)) -- WBR, Timur
Janet - Try x2 <- as.numeric(as.character(x)) hist(x2) I'm not a Windows user, so I can't test this before sending. It might solve the problem, might not. (Flame !! : This is just ONE MORE example of the difficulties caused by the default behavior of read.table() to make things into factors. I sincerely wish the default were to preserve character data as character.) - tom blackwell - u michigan medical school - ann arbor - On Mon, 15 Mar 2004, Janet Gannon wrote:> I am reading a list of numbers from my clipboard, and have been > successful, except I can't make a histogram as R doesn't recognize my > variable as numeric. I know I need to use "as.is", but the specifics > escape me. > > I have used x<-read.table("clipboard", header=F) to import from a txt > file. How do make this numeric? Thanks, J. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Janet Gannon wrote:> I am reading a list of numbers from my clipboard, and have been > successful, except I can't make a histogram as R doesn't recognize my > variable as numeric. I know I need to use "as.is", but the specifics > escape me. > I have used x<-read.table("clipboard", header=F) to import from a txtIn normal circumstances, your example works, but sometimes you need special options, e.g sep="\t" if you have missing values or dec="," in countries with comma as decimal delimiter. Another possibility is, that read.table has converted your data into factors, then convert them back, via: x$V1 <- as.numeric(as.character(x$V1)) etc... For more options please read the R Data Import/Export manual. Thomas
Janet Gannon wrote:> I am reading a list of numbers from my clipboard, and have been > successful, except I can't make a histogram as R doesn't recognize my > variable as numeric. I know I need to use "as.is", but the specifics > escape me. > I have used x<-read.table("clipboard", header=F) to import from a txt > file. How do make this numeric? Thanks, J.So the elements of your data frame "x" are not numeric? read.table() auto-detects which variables are numeric or not, so there's something strange with your textfile, I guess. as.numeric(x$V1) converts the first element in your data.frame x to numeric. Uwe Ligges
> From: Peter Dalgaard > Sent: Monday, March 15, 2004 3:51 PM > To: Timur Elzhov > Cc: r-help at stat.math.ethz.ch; Janet Gannon > Subject: Re: [R] Simple numeric "as.is" question > > > Timur Elzhov <Timur.Elzhov at jinr.ru> writes: > > > On Mon, Mar 15, 2004 at 09:04:05AM -0500, Janet Gannon wrote: > > > > > I am reading a list of numbers from my clipboard, and have been > > > successful, except I can't make a histogram as R doesn't > recognize my > > > variable as numeric. I know I need to use "as.is", but > the specifics > > > escape me. > > > > > > I have used x<-read.table("clipboard", header=F) to > import from a txt > > > file. How do make this numeric? Thanks, J. > > > > x <- read.table("clipboard", header=F) > > x <- as.data.frame(lapply(x, as.numeric)) > > This is almost always wrong. If a column was erroneously converted to > a factor, it comes out as 1,2,3,4 even if the original values were > 3.5,4,5.6,10.2. as.numeric(as.character(....)) is a better bet but a > bit silly for those columns that were numeric to begin with. > > You might try > > lapply(x,function(x) if (is.numeric(x)) x else > as.numeric(as.character(x)) ) > > but probably better is to find out *why* a variable is not read as > numeric. It's usually because an least one value is not a number.... which is why I suggested use scan() instead of read.table(), which defaults to numeric input, and would complain about non-numeric input. Re-reading the original message, there's really not enough info to tell what's wrong. How does Janet know that the data read in are not numeric? What types were they exactly? Are there more than one variable in the input? Andy> -- > O__ ---- Peter Dalgaard Blegdamsvej 3 > c/ /'_ --- Dept. of Biostatistics 2200 Cph. N > (*) \(*) -- University of Copenhagen Denmark Ph: > (+45) 35327918 > ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: > (+45) 35327907 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments,...{{dropped}}