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
G'day Simone, On Sun, 30 Nov 2008 11:05:13 +0100 Simone Gabbriellini <simone.gabbriellini at gmail.com> 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 Turlach Tel.: +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: statba at nus.edu.sg Singapore 117546 http://www.stat.nus.edu.sg/~statba
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: > > 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 lengthMake 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 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.-- 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
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: > > 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 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. >-- 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
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 < simone.gabbriellini@gmail.com> 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]]