2

I use large sets of contingency tables by looping table(). Simple problem: columns containing only 0's are omitted in the output. Can this be adjusted using table()?

Example:

data

1 0
0 0
1 0
0 0
0 0
1 0
1 0
1 0
1 0

The output I get:

table(data)
    0
  0 3
  1 6

The output I need:

    0 1
  0 3 0
  1 6 0

Thanks.

lardensis
  • 21
  • 1
  • Here's one way: `data2 <- data; data2[] <- lapply(data,factor,levels=as.character(0:1)); table(data2)` I suspect this question was asked before; going to take a look. – Frank May 18 '15 at 19:30
  • @Frank your comment is an answer. You should add it as an answer. That way Stackoverflow works best. – Jesper Rønn-Jensen May 18 '15 at 19:31
  • @JesperRønn-Jensen Should I answer even if it's a dupe? – Frank May 18 '15 at 19:31
  • @Frank if you can find a dupe, vote to close as such. – Roman Luštrik May 18 '15 at 19:32
  • Still looking. Top candidate: http://stackoverflow.com/q/5558745/1191259 – Frank May 18 '15 at 19:33
  • @Frank: I think [this](http://stackoverflow.com/q/8461140/3005513) is the closest. – Alex A. May 18 '15 at 19:49
  • @AlexA. Hm, I still lean towards the other one (since we have to modify multiple dimensions of the `table`) and can't switch my own vote now, besides. Other voters'll see both suggested dupes, though, and can consider it. By the way, you can edit your auto-generated flag-as-dupe comment (e.g., to @ someone or edit the text). – Frank May 18 '15 at 19:55
  • 1
    @Frank Your answer (first comment) works perfectly, thanks. – lardensis May 18 '15 at 20:22

0 Answers0