Reputation: 11
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")
zipcode_table_B11 = cbraw %>%
group_by(QS2B, B11) %>%
summarize(n()) %>%
table()
zipcode_table_B11
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
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.
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
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
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