Max Hermanson
Max Hermanson

Reputation: 11

Problems with Table() function

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!

Upvotes: 1

Views: 1259

Answers (1)

Gregor Thomas
Gregor Thomas

Reputation: 146224

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))

Upvotes: 1

Related Questions