Create frequency tables for multiple factor columns in R

后端 未结 3 786
抹茶落季
抹茶落季 2020-11-28 15:13

I am a novice in R. I am compiling a separate manual on the syntax for the common functions/features for my work. My sample dataframe as follows:

x.sample &l         


        
相关标签:
3条回答
  • 2020-11-28 15:46

    Coming a bit late, but here's a reshape2 possible solution. It could have been very straightforward with recast but we need to handle empty factor levels here so we need to specify both factorsAsStrings = FALSE within melt and drop = FALSE within dcast, while recast can't pass arguments to melt (only to dcast), so here goes

    library(reshape2)
    x.sample$indx <- 1 
    dcast(melt(x.sample, "indx", factorsAsStrings = FALSE), value ~ variable, drop = FALSE)
    #             value Q9_A Q9_B Q9_C
    # 1            Impt    1    0    0
    # 2         Neutral    0    0    0
    # 3 Not Impt at all    3    4    4
    # 4   Somewhat Impt    0    0    0
    # 5       Very Impt    6    6    6
    

    If we wouldn't care about empty levels a quick solution would be just

    recast(x.sample, value ~ variable, id.var = "indx")
    #             value Q9_A Q9_B Q9_C
    # 1            Impt    1    0    0
    # 2 Not Impt at all    3    4    4
    # 3       Very Impt    6    6    6
    

    Alternatively, if speed is a concern, we can do the same using data.atble

    library(data.table)
    dcast(melt(setDT(x.sample), measure.vars = names(x.sample), value.factor = TRUE), 
               value ~ variable, drop = FALSE)
    #              value Q9_A Q9_B Q9_C
    # 1:            Impt    1    0    0
    # 2:         Neutral    0    0    0
    # 3: Not Impt at all    3    4    4
    # 4:   Somewhat Impt    0    0    0
    # 5:       Very Impt    6    6    6
    
    0 讨论(0)
  • 2020-11-28 15:53

    You were nearly there. Just one small change in your function would have got you there. The x in function(x) ... needs to be passed through to the table() call:

    levs <- c("Not Impt at all", "Somewhat Impt", "Neutral", "Impt", "Very Impt")
    sapply(x.sample, function(x) table(factor(x, levels=levs, ordered=TRUE)))
    

    A little re-jig of the code might make it a bit easier to read too:

    sapply(lapply(x.sample,factor,levels=levs,ordered=TRUE), table)
    
    #                Q9_A Q9_B Q9_C
    #Not Impt at all    3    4    4
    #Somewhat Impt      0    0    0
    #Neutral            0    0    0
    #Impt               1    0    0
    #Very Impt          6    6    6
    
    0 讨论(0)
  • 2020-11-28 15:56

    Why not just:

    > sapply(x.sample, table)
                    Q9_A Q9_B Q9_C
    Impt               1    0    0
    Neutral            0    0    0
    Not Impt at all    3    4    4
    Somewhat Impt      0    0    0
    Very Impt          6    6    6
    

    Let's call it 'tbl';

    tbl[ order(match(rownames(tbl), c("Not Impt at all", "Somewhat Impt", 
                                      "Neutral", "Impt", "Very Impt")) )   , ]
                    Q9_A Q9_B Q9_C
    Not Impt at all    3    4    4
    Somewhat Impt      0    0    0
    Neutral            0    0    0
    Impt               1    0    0
    Very Impt          6    6    6
    
    0 讨论(0)
提交回复
热议问题