0

I have the result of a survey about 5 questions that can be answered by "Yes" or "Not" :

  question1 question2 question3 question4 question5
1       Yes       Yes       Yes       Yes       Yes
2       Yes        No       Yes        No       Yes
3       Yes       Yes        No        No       Yes
4       Yes        No        No        No        No
5       Yes       Yes        No        No       Yes

My goal is to build a dataframe : one line for each question, one column "Yes_n" that counts how many "Yes" and one column "No_n" that counts how many "No" but I can't do it..

Do you have any idea to build it please?


data

question1 <- c("Yes","Yes","Yes","Yes","Yes")
question2 <- c("Yes","No","Yes","No","Yes")
question3 <- c("Yes","Yes","No","No","No")
question4 <- c("Yes","No","No","No","No")
question5 <- c("Yes","Yes","Yes","No","Yes")

df<-as.data.frame(cbind(question1,question2,question3,question4,question5))
3
  • Please provide a dput() of your data for reproducibility Commented May 18, 2022 at 12:32
  • @Benson_YoureFired i've modified my question Commented May 18, 2022 at 12:37
  • 2
    table(rev(stack(df)))
    – lotus
    Commented May 18, 2022 at 12:48

1 Answer 1

1

The easiest would be to use colSums:

data.frame(Yes_n = colSums(df == "Yes"),
           No_n = colSums(df == "No"))

          Yes_n No_n
question1     5    0
question2     3    2
question3     2    3
question4     1    4
question5     4    1

with table:

dplyr::bind_rows(sapply(df, table), .id = "Question")

With tidyverse functions

library(tidyverse)
df %>% 
  pivot_longer(everything()) %>% 
  group_by(name) %>% 
  count(value) %>% 
  pivot_wider(names_from = value, values_from = n, values_fill = 0)

# A tibble: 5 × 3
# Groups:   name [5]
  name        Yes    No
  <chr>     <int> <int>
1 question1     5     0
2 question2     3     2
3 question3     2     3
4 question4     1     4
5 question5     4     1

Not the answer you're looking for? Browse other questions tagged or ask your own question.