Search code examples
rsurveysurveymonkey

prop.table with multiple variable


I'm looking to recreate a table that is populated by SurveyMonkey (example: https://i.sstatic.net/5MyV1.jpg) - along the left-hand side are the questions (e.g. How important is Factor X?) and then the discrete variable is always the same: Not At All, Slightly...etc.

What is the best way to take data that is in the format(column #s are questions and end in the resulting prop.table?:

            Q1        Q2        Q3        Q4
         Very       Very       Very Moderately
   Moderately   Slightly  Extremely Moderately
   Not at all       Very       Very   Slightly
   Not at all Moderately Not at all Not at all
    Extremely  Extremely  Extremely  Extremely
     Slightly       Very  Extremely   Slightly

Solution

  • Tidy your data first. Assuming your original dataset is in a data.frame called df:

    library(tidyr)
    
    df.tidy <- gather(df, question, result)
    
    prop.table(table(df.tidy$question, df.tidy$result))
    #     Extremely Moderately Not at all   Slightly       Very
    # Q1 0.04166667 0.04166667 0.08333333 0.04166667 0.04166667
    # Q2 0.04166667 0.04166667 0.00000000 0.04166667 0.12500000
    # Q3 0.12500000 0.00000000 0.04166667 0.00000000 0.08333333
    # Q4 0.04166667 0.08333333 0.04166667 0.08333333 0.00000000
    

    # Additional stuff to look at to check your understanding...
    #
    # df.tidy
    # table(df.tidy$question, df.tidy$result)
    # prop.table(table(df.tidy$question, df.tidy$result), margin = 1)
    # prop.table(table(df.tidy$question, df.tidy$result), margin = 2)
    

    If you need "by-question" percentages, you will want to use margin = 1 in the call to prop.table() -- see ?prop.table