Re: [R] simple question with table()
Berwin A Turlach wrote: G'day Simone, On Sun, 30 Nov 2008 11:05:13 +0100 Simone Gabbriellini <[EMAIL PROTECTED]> wrote: my problem should be easy to fix, but I couldn't find a solution by myself... In my survey, there is a question with 14 possible answers. None of the respondents choose the 13th answer, so when I table() the results, R says: [...] 13 is missing... anyone knows how to tell table() that there are 14 modalities in the answers? The easiest way is probably to turn your data into a factor with the appropriate set of levels: R> dat <- sample(c(1:12,14), 100, replace=TRUE) R> table(factor(dat, levels=min(dat):max(dat))) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 9 5 4 7 7 11 8 10 9 8 6 11 0 5 that was once the solution of one of my colleagues and I find it somewhat nicer than the one I came up with: R> rng <- min(dat):max(dat) R> res <- colSums(outer(dat, min(dat):max(dat), "==")) R> names(res) <- rng R> res 1 2 3 4 5 6 7 8 9 10 11 12 13 14 9 5 4 7 7 11 8 10 9 8 6 11 0 5 Notice, though, that you don't always want to have the limits being data-dependent either. E.g. if you have multiple questions of the variety "on a scale of 1:5 how do you feel...", you presumably want your barcharts on the same scale even if some questions are all 1's or all 5's. So the straightforward table(factor(x,levels=1:14)) might be preferable. -- O__ Peter Dalgaard Ă˜ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ 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.
Re: [R] simple question with table()
you have to use a factor, e.g., x <- sample((1:14)[-13], 100, TRUE) f <- factor(x, levels = 1:14) table(f) I hope it helps. Best, Dimitris Simone Gabbriellini wrote: Dear List, my problem should be easy to fix, but I couldn't find a solution by myself... In my survey, there is a question with 14 possible answers. None of the respondents choose the 13th answer, so when I table() the results, R says: 12345678910111214 3152721407822822117 13 is missing... anyone knows how to tell table() that there are 14 modalities in the answers? I tried with responseName=c("1","2","3","4","5","6","7","8","9","10","11","12","13","14") but all I have is: error in table all the arguments must have the same length thank you, Simone __ 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. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 __ 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.
Re: [R] simple question with table()
Dear Simone, Try this: x=c(1,1,2,3,4,3,2,2,2,1,2,3,4,4,3) table(factor(x,levels=1:5)) 1 2 3 4 5 3 5 4 3 0 HTH, Jorge On Sun, Nov 30, 2008 at 5:05 AM, Simone Gabbriellini < [EMAIL PROTECTED]> wrote: > Dear List, > > my problem should be easy to fix, but I couldn't find a solution by > myself... > > In my survey, there is a question with 14 possible answers. None of the > respondents choose the 13th answer, so when I table() the results, R says: > > 1 2 3 4 5 6 7 8 9 10 > 11 12 14 > 31 52 7 21 40 7 8 2 28 2 > 2 1 17 > > 13 is missing... anyone knows how to tell table() that there are 14 > modalities in the answers? I tried with > > > responseName=c("1","2","3","4","5","6","7","8","9","10","11","12","13","14") > > but all I have is: > > error in table > all the arguments must have the same length > > thank you, > Simone > > __ > 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. > [[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.
Re: [R] simple question with table()
On 11/30/2008 5:05 AM, Simone Gabbriellini wrote: > Dear List, > > my problem should be easy to fix, but I couldn't find a solution by > myself... > > In my survey, there is a question with 14 possible answers. None of the > respondents choose the 13th answer, so when I table() the results, R says: > > 12345678910111214 > 3152721407822822117 > > 13 is missing... anyone knows how to tell table() that there are 14 > modalities in the answers? I tried with > > responseName=c("1","2","3","4","5","6","7","8","9","10","11","12","13","14") > > > but all I have is: > > error in table > all the arguments must have the same length Make the responses to the question a factor and set the levels of the factor to include all 14 possible answers. For example: > table(factor(rep(c('1','2'), each=7), levels=c('1','2','3'))) 1 2 3 7 7 0 ?factor > thank you, > Simone > > __ > 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. -- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894 __ 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.
Re: [R] simple question with table()
G'day Simone, On Sun, 30 Nov 2008 11:05:13 +0100 Simone Gabbriellini <[EMAIL PROTECTED]> wrote: > my problem should be easy to fix, but I couldn't find a solution by > myself... > > In my survey, there is a question with 14 possible answers. None of > the respondents choose the 13th answer, so when I table() the > results, R says: [...] > 13 is missing... anyone knows how to tell table() that there are 14 > modalities in the answers? The easiest way is probably to turn your data into a factor with the appropriate set of levels: R> dat <- sample(c(1:12,14), 100, replace=TRUE) R> table(factor(dat, levels=min(dat):max(dat))) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 9 5 4 7 7 11 8 10 9 8 6 11 0 5 that was once the solution of one of my colleagues and I find it somewhat nicer than the one I came up with: R> rng <- min(dat):max(dat) R> res <- colSums(outer(dat, min(dat):max(dat), "==")) R> names(res) <- rng R> res 1 2 3 4 5 6 7 8 9 10 11 12 13 14 9 5 4 7 7 11 8 10 9 8 6 11 0 5 HTH. Best wishes, Berwin === Full address = Berwin A TurlachTel.: +65 6516 4416 (secr) Dept of Statistics and Applied Probability+65 6516 6650 (self) Faculty of Science FAX : +65 6872 3919 National University of Singapore 6 Science Drive 2, Blk S16, Level 7 e-mail: [EMAIL PROTECTED] Singapore 117546http://www.stat.nus.edu.sg/~statba __ 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.
[R] simple question with table()
Dear List, my problem should be easy to fix, but I couldn't find a solution by myself... In my survey, there is a question with 14 possible answers. None of the respondents choose the 13th answer, so when I table() the results, R says: 1 2 3 4 5 6 7 8 9 10 11 12 14 31 52 7 21 40 7 8 2 28 2 2 1 17 13 is missing... anyone knows how to tell table() that there are 14 modalities in the answers? I tried with responseName =c("1","2","3","4","5","6","7","8","9","10","11","12","13","14") but all I have is: error in table all the arguments must have the same length thank you, Simone __ 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.