Dear everybody! I am analysing data from an enquette. The answers are either A or B. How can I draw a histogram without transforming the data from characters to numbers? If the data are saved in a list M, hist(M[,1]) returns: Error in hist.default(M[, 1]) : `x' must be numeric Execution halted Thank you in advance!
On Wed, 20 Apr 2005 17:37:04 +0200 Mag. Ferri Leberl wrote:> Dear everybody! > I am analysing data from an enquette. The answers are either A or B. > How can I draw a histogram without transforming the data from > characters to numbers? If the data are saved in a list M, hist(M[,1]) > returns: > > Error in hist.default(M[, 1]) : `x' must be numeric > Execution haltedI think you want a barplot not a histogram. ?barplot Z> Thank you in advance! > > ______________________________________________ > 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 >
"Mag. Ferri Leberl" <ferri.leberl at gmx.at> writes:> Dear everybody! > I am analysing data from an enquette. The answers are either A or B. How can I > draw a histogram without transforming the data from characters to numbers? If > the data are saved in a list M, hist(M[,1]) returns: > > Error in hist.default(M[, 1]) : `x' must be numeric > Execution halted > > Thank you in advance!You can't. Histograms are density estimates for continuous outcomes. You can do a barplot, however. -- 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
> From: Mag. Ferri Leberl > > Dear everybody! > I am analysing data from an enquette. The answers are either > A or B. How can I > draw a histogram without transforming the data from > characters to numbers? If > the data are saved in a list M, hist(M[,1]) returns: > > Error in hist.default(M[, 1]) : `x' must be numeric > Execution haltedYou can try:> hist.factor <- function(x, ...) barplot(table(x), ...) > f <- factor(sample(c("A", "B"), 50, replace=TRUE)) > hist(f)HTH, Andy> Thank you in advance!
As Achim said, I would use a barplot instead of an hist.
Here's how I would do it:
vect<-c("a","b","a","b","b","b","a","a","a")
a<-length(vect[vect=="a"])
b<-length(vect[vect=="b"])
barplot(c(a,b),names.arg=(c("A","B")))
Neuro the Super Hero
>From: "Mag. Ferri Leberl" <ferri.leberl at gmx.at>
>Reply-To: ferri.leberl at gmx.at
>To: r-help at stat.math.ethz.ch
>Subject: [R] Histogram
>Date: Wed, 20 Apr 2005 17:37:04 +0200
>
>Dear everybody!
>I am analysing data from an enquette. The answers are either A or B. How
>can I
>draw a histogram without transforming the data from characters to numbers?
>If
>the data are saved in a list M, hist(M[,1]) returns:
>
>Error in hist.default(M[, 1]) : `x' must be numeric
>Execution halted
>
>Thank you in advance!
>
>______________________________________________
>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
Of course Andy meant hist.factor(f) In particular you should note that Andy uses the table function to "transform ... the data from characters to numbers" Tom> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of Liaw, Andy > Sent: Thursday, 21 April 2005 12:14 AM > To: 'ferri.leberl at gmx.at'; r-help at stat.math.ethz.ch > Subject: RE: [R] Histogram > > > > From: Mag. Ferri Leberl > > > > Dear everybody! > > I am analysing data from an enquette. The answers are either > > A or B. How can I > > draw a histogram without transforming the data from > > characters to numbers? If > > the data are saved in a list M, hist(M[,1]) returns: > > > > Error in hist.default(M[, 1]) : `x' must be numeric > > Execution halted > > You can try: > > > hist.factor <- function(x, ...) barplot(table(x), ...) > > f <- factor(sample(c("A", "B"), 50, replace=TRUE)) > > hist(f) > > HTH, > Andy > > > Thank you in advance! > > ______________________________________________ > 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 >
Mea Culpa I did copy and paste your code to make sure it was an error. I'll go and do it again before I reply. It worked as you expected it to. So I located where I had done it and this is what I found> f <- factor(sample(c("A", "B"), 50, replace=TRUE)) > hist(f)Error in hist.default(f) : 'x' must be numeric Since I had more than one session open I had inadvertantly pasted the> hist.factor <- function(x, ...) barplot(table(x), ...)into another window. For some reason or other I have always had finger trouble with the "paste commands only" in the windows GUI, so I do it one line at a time or past into Tinn-R and select with the alt key. I guess its time for me to actually understand methods, because it's patently obvious I don't even though I was beginning to think I had a handle on them. Tom> -----Original Message----- > From: Liaw, Andy [mailto:andy_liaw at merck.com] > Sent: Thursday, 21 April 2005 10:06 AM > To: Mulholland, Tom > Subject: RE: [R] Histogram > > > Have you tried it? hist.factor() as defined would be the > hist method for > the factor class, so hist(f) would work if f is a factor. > > Andy > > > From: Mulholland, Tom > > > > Of course Andy meant hist.factor(f) > > > > In particular you should note that Andy uses the table > > function to "transform ... the data from characters to numbers" > > > > Tom > > > > > -----Original Message----- > > > From: r-help-bounces at stat.math.ethz.ch > > > [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of Liaw, Andy > > > Sent: Thursday, 21 April 2005 12:14 AM > > > To: 'ferri.leberl at gmx.at'; r-help at stat.math.ethz.ch > > > Subject: RE: [R] Histogram > > > > > > > > > > From: Mag. Ferri Leberl > > > > > > > > Dear everybody! > > > > I am analysing data from an enquette. The answers are either > > > > A or B. How can I > > > > draw a histogram without transforming the data from > > > > characters to numbers? If > > > > the data are saved in a list M, hist(M[,1]) returns: > > > > > > > > Error in hist.default(M[, 1]) : `x' must be numeric > > > > Execution halted > > > > > > You can try: > > > > > > > hist.factor <- function(x, ...) barplot(table(x), ...) > > > > f <- factor(sample(c("A", "B"), 50, replace=TRUE)) > > > > hist(f) > > > > > > HTH, > > > Andy > > > > > > > Thank you in advance! > > > > > > ______________________________________________ > > > 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 > > > > > > > > > > > > > -------------------------------------------------------------- > ---------------- > Notice: This e-mail message, together with any attachments, > contains information of Merck & Co., Inc. (One Merck Drive, > Whitehouse Station, New Jersey, USA 08889), and/or its > affiliates (which may be known outside the United States as > Merck Frosst, Merck Sharp & Dohme or MSD and in Japan, as > Banyu) that may be confidential, proprietary copyrighted > and/or legally privileged. It is intended solely for the use > of the individual or entity named on this message. If you > are not the intended recipient, and have received this > message in error, please notify us immediately by reply > e-mail and then delete it from your system. > -------------------------------------------------------------- > ---------------- >