0

I'm new to R and trying to do some exploratory analysis. I have a dataset with multiple columns, 3 of them look like this

Q1 Q2 Q3
1  6  2
2  6  7
2  6  6
6  6  6
7  1  1
6  6  1

where Qs are yes/no questions and 1,2,6,7 are encoded for "Yes", "No", "Don't know", and "Valid skip"

I want to create small multiples plot from these 3 columns that shows the frequency of each type of answer grouped by question. The approach I'm trying to do is to transform these columns to a new data frame with 3 variables: Question (that takes 3 values Q1, Q2, Q3), Answer (takes 4 values 1, 2, 6, 7), and Frequency, then I can create small multiples.

Could you show me how to do this transformation? And if you know other ways to create the chart, please share as I would love to know different ways to do it.

Thanks a lot!

  • Greetings! Usually it is helpful to provide a minimally reproducible dataset for questions here so people can troubleshoot your problems (rather than a table or screenshot for example). One way of doing is by using the `dput` function on the data or a subset of the data you are using, then pasting the output into your question. You can find out how to use it here: https://youtu.be/3EID3P1oisg – Shawn Hemelstrand Nov 17 '22 at 01:22

1 Answers1

0

You could try using the dplyr and tidyr libraries:

library(tidyr)
library(dplyr)

df %>% 
  pivot_longer(Q1:Q3, names_to = "question", values_to = "answer") %>% 
  group_by(question) %>% 
  count(answer) %>%
  rename(frequency = n)

Output:

   question answer frequency
   <chr>     <int>     <int>
 1 Q1            1         1
 2 Q1            2         2
 3 Q1            6         2
 4 Q1            7         1
 5 Q2            1         1
 6 Q2            6         5
 7 Q3            1         2
 8 Q3            2         1
 9 Q3            6         2
10 Q3            7         1

Data

df <- read.table(text = "Q1 Q2 Q3
1  6  2
2  6  7
2  6  6
6  6  6
7  1  1
6  6  1", header = TRUE)
jpsmith
  • 11,023
  • 5
  • 15
  • 36