How to use R for multiple select questions?

后端 未结 3 575
攒了一身酷
攒了一身酷 2021-02-04 19:03

I am trying to figure out how to analyze multiple select/multiple responses (i.e., \'select all that apply\') questions in a survey I recently conducted.

SPSS has nice c

3条回答
  •  无人及你
    2021-02-04 19:24

    I recently wrote a quick function to deal with these. You can easily modify it to add proportion of total responses too.

    set.seed(1)
    dat <- data.frame(resp = 1:10,
                      like1 = sample(0:1, 10, TRUE),
                      like2 = sample(0:1, 10, TRUE),
                      like3 = sample(0:1, 10, TRUE))
    

    The function:

    multi.freq.table = function(data, sep="", dropzero=FALSE, clean=TRUE) {
      # Takes boolean multiple-response data and tabulates it according
      #   to the possible combinations of each variable.
      #
      # See: http://stackoverflow.com/q/11348391/1270695
    
      counts = data.frame(table(data))
      N = ncol(counts)
      counts$Combn = apply(counts[-N] == 1, 1, 
                           function(x) paste(names(counts[-N])[x],
                                             collapse=sep))
      if (isTRUE(dropzero)) {
        counts = counts[counts$Freq != 0, ]
      } else if (!isTRUE(dropzero)) {
        counts = counts
      }
      if (isTRUE(clean)) {
        counts = data.frame(Combn = counts$Combn, Freq = counts$Freq)
      } 
      counts
    }
    

    Apply the function:

    multi.freq.table(dat[-1], sep="-")
    #               Combn Freq
    # 1                      1
    # 2             like1    2
    # 3             like2    2
    # 4       like1-like2    2
    # 5             like3    1
    # 6       like1-like3    1
    # 7       like2-like3    0
    # 8 like1-like2-like3    1
    

    Hope this helps! Otherwise, show some examples of desired output or describe some features, and I'll see what can be added.

    Update

    After looking at the output of SPSS for this online, it seems like the following should do it for you. This is easy enough to wrap into a function if you need to use it a lot.

    data.frame(Freq = colSums(dat[-1]),
               Pct.of.Resp = (colSums(dat[-1])/sum(dat[-1]))*100,
               Pct.of.Cases = (colSums(dat[-1])/nrow(dat[-1]))*100)
    #       Freq Pct.of.Resp Pct.of.Cases
    # like1    6    42.85714           60
    # like2    5    35.71429           50
    # like3    3    21.42857           30
    

提交回复
热议问题