I have a table with respondents and their answers on the survey and companies they belong to. I want to know how many respondents answered the question by each company.
Data:
structure(list(respondent = c("a", "a", "a2", "a", "b", "b",
"c", "c", "c", "d", "d3", "d", "d", "d2", "d", "e", "e", "e",
"f", "g"), question = c("q1", "q2", "q1", "q2", "q1", "q2", "q1",
"q2", "q1", "q2", "q1", "q2", "q1", "q2", "q1", "q2", "q1", "q2",
"q1", "q2"), answer = c(1, 1, 0, 1, 1, 0, -1, 1, -1, -1, 1, 0,
0, -1, -1, -1, 1, 1, -1, 1), name = c("AU", "AU", "GU", "AU",
"AU", "AU", "BU", "BU", "CU", "DU", "BU", "DU", "DU", "EU", "DU",
"EU", "EU", "EU", "FU", "GU")), row.names = c(NA, -20L), class = c("tbl_df",
"tbl", "data.frame"))
You see, I try to use tidyverse
functions, to be more precise, summarise()
. I write the followin code:
a <- dat %>%
group_by(respondent, name) %>%
summarise(q = n())
But the output is not something I really need. I want to recieve the dataframe where there's a column with name
and another column with the number of unique respondents who attributed to this name in the original dataset. I realize that is something is wrong with my summarise
, but I can't find the clue.
I want to get something like that
name N
AU 2
BU 2
CU 1
DU 1
EU 1
GU 2