0

I have output from a survey and the responses from each person are in one long row. Questions are labeled with a prefix of Q1, Q2, Q3 of varying lengths depending on the number of answer options.

Example of data here

I would like to create graphs and summarize results by question in R but struggling to find a way to identify the different prefixes.

I tried this

data2 <- adply(data[grepl("Q", colnames(data))],2, function(x)
    data.frame(Count=as.data.frame(table(x))[2,2],
    Perc=as.data.frame(prop.table(table(x)))[2,2]))`

But that just gave me each column as a row with the count and percentage. That is the closest I have gotten to what I want.

r2evans
  • 141,215
  • 6
  • 77
  • 149
T Hohoff
  • 1
  • 1
  • 8
    Welcome to SO! It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of our data to run your code. Please do not post an image of code/data/errors [for these reasons](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question/285557#285557). Just include the data directly using e.g. `dput(head(x))`. – stefan Aug 28 '23 at 14:44
  • 8
    Please don't post data as images. We cannot copy/paste these into our consoles to test or verify possible solutions. Instead, use `dput(data)` to get a reproducible copy-pasteable version of your data. At the moment, we cannot even see your complete column names, and text parsing tasks like this are dependent on knowing the _exact_ strings we are working with. Thanks. – Allan Cameron Aug 28 '23 at 14:45
  • 2
    It seems likely that you want to reshape this data from its current wide format to a longer format. See https://stackoverflow.com/q/2185252/3358272; (names_pattern) https://stackoverflow.com/q/68058000/3358272; (multiple value columns) https://stackoverflow.com/q/61367186/3358272, https://stackoverflow.com/q/71530939/3358272 for many discussions on reshaping. – r2evans Aug 28 '23 at 15:18

0 Answers0