1

Am new to R, and am required to self-learn how to use it for a task at my current job. First post on StackOverflow too, so forgive my omitting of any essential info.

I am finding that I am not using table() function properly, for it's generating tables that do not include all of the values that should be included. Here's my code/output:

    library(tidyverse)
    library(dplyr)
    cbraw <- read.csv("Cbay.csv")

QS2B = survey zipcode

B11 = Specific survey question

    zipcode_table_B11 = cbraw %>% 
      group_by(QS2B, B11) %>% 
      summarize(n()) %>% 
      table()
    zipcode_table_B11

Output

      B11
QS2B    1 2 3 4 5 8 9
  12064 1 0 0 0 0 0 0
  12115 0 0 0 0 0 0 0
  12116 1 0 0 0 0 0 0
  12155 0 0 1 0 0 0 0

This is the excel data (AKA cbraw):

QS2B    B11
12064   1.00
12115   
12116   1.00
12155   1.00
12155   3.00
12155   1.00

Problem with QS2B column, value 12155

Now, notice how the 12155's on the df have the values 1,3, and 1. Only the '3' is being counted in my output, however.

actual output:

B11
QS2B    1 2 3 4 5 8 9
  12064 1 0 0 0 0 0 0
  12115 0 0 0 0 0 0 0
  12116 1 0 0 0 0 0 0
  12155 0 0 1 0 0 0 0

Desired output:

B11
QS2B    1 2 3 4 5 8 9
  12064 1 0 0 0 0 0 0
  12115 0 0 0 0 0 0 0
  12116 1 0 0 0 0 0 0
  12155 2 0 1 0 0 0 0

Does anyone know why the 1's are not being counted in my table?

Any help would be greatly appreciated!

Community
  • 1
  • 1
  • Please do not post an image of code/data/errors: it cannot be copied or searched (SEO), it breaks screen-readers, and it may not fit well on some mobile devices. Ref: https://meta.stackoverflow.com/a/285557 (and https://xkcd.com/2116/). Please just include the code, console output, or data (e.g., `dput(head(x))` or `data.frame(...)`) directly. – r2evans Apr 06 '20 at 17:12
  • Welcome to SO, Max! Yeah, posting images of code (esp with syntax problems, `?var <- read.csv(...)` is not right) is going to reduce the amount of help you can get substantially. If the error is not obvious, then you're telling us to spend time transcribing your code ... and for me, that's an immediate "discard the question, I'm out". Most answerers here are doing so voluntarily, do not get paid for this, so ... please read how to ask questions *well*: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Apr 06 '20 at 17:15
  • And finally ... *what's wrong*? You say it doesn't include all of the values, but nothing in your second image is not included in the first. I think you need to clearly state what is missing and why. It would help to reduce your problem (smaller sample data), and provide that sample data (either as the output of `dput(head(x))` or `data.frame(...)`) along with what you think the output should look like. – r2evans Apr 06 '20 at 17:22
  • I just formatted your question - you can add code formatting by putting 3 backticks on a line before a code block and after a code block, or highlighting the code blocking and clicking the `{}` button in the editor. – Gregor Thomas Apr 06 '20 at 18:43
  • Haha, thank you for you patience guys. I realized it was still not properly formatted after I saved my edits to @r2evans and was in the middle of sorting that out. – Max Hermanson Apr 06 '20 at 18:48
  • Also, don't bother appending edit notes to your question. We've got the comments down here, and anyone interested can view the edit history by clicking on the "edited X time ago" link. – Gregor Thomas Apr 06 '20 at 18:51

1 Answers1

1

I think you're overcomplicating. table counts up occurrences, and puts it in a wide format showing all possible combinations. group_by() %>% summarize(n()) counts up occurrences and puts the results in a long format, showing only combinations that occurred. You don't need both. Since you want a "wide" format output, table is better. I think what you want is this:

with(zipcode_table_B11, table(QS2B, B11))
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294