[R] simple question with table()
Peter Dalgaard
p.dalgaard at biostat.ku.dk
Sun Nov 30 11:42:34 CET 2008
Berwin A Turlach wrote:
> 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
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
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help
mailing list