Creating frequency tables for multiple columns of factors in R

I am new to R. I am writing a separate syntax guide for common functions / functions for my work. My sample data is as follows:

x.sample <- structure(list(Q9_A = structure(c(5L, 3L, 5L, 3L, 5L, 3L, 1L, 5L, 5L, 5L), .Label = c("Impt", "Neutral", "Not Impt at all", "Somewhat Impt", "Very Impt"), class = "factor"), Q9_B = structure(c(5L, 5L, 5L, 3L, 5L, 5L, 3L, 5L, 3L, 3L), .Label = c("Impt", "Neutral", "Not Impt at all", "Somewhat Impt", "Very Impt"), class = "factor"), Q9_C = structure(c(3L, 5L, 5L, 3L, 5L, 5L, 3L, 5L, 5L, 3L ), .Label = c("Impt", "Neutral", "Not Impt at all", "Somewhat Impt", "Very Impt"), class = "factor")), .Names = c("Q9_A", "Q9_B", "Q9_C"), row.names = c(NA, 10L), class = "data.frame") > x.sample Q9_A Q9_B Q9_C 1 Very Impt Very Impt Not Impt at all 2 Not Impt at all Very Impt Very Impt 3 Very Impt Very Impt Very Impt 4 Not Impt at all Not Impt at all Not Impt at all 5 Very Impt Very Impt Very Impt 6 Not Impt at all Very Impt Very Impt 7 Impt Not Impt at all Not Impt at all 8 Very Impt Very Impt Very Impt 9 Very Impt Not Impt at all Very Impt 10 Very Impt Not Impt at all Not Impt at all 

There are 21 columns in my original data frame.

If I want to find the average value (considering this as an ordinal parameter):

 > sapply(x.sample,function(x) mean(as.numeric(x), na.rm=TRUE)) Q9_A Q9_B Q9_C 4.0 4.2 4.2 

I would like to give a frequency table table for ALL variables in my framework. I searched the internet and many forums and saw that the closest team is using sapply for this. But when I did this, he gave all 0s.

 > sapply(x.sample,function(x) table(factor(x.sample, levels=c("Not Impt at all", "Somewhat Impt", "Neutral", "Impt", "Very Impt"), ordered=TRUE))) Q9_A Q9_B Q9_C Not Impt at all 0 0 0 Somewhat Impt 0 0 0 Neutral 0 0 0 Impt 0 0 0 Very Impt 0 0 0 

Question How can I use sapply to tab the frequency diagram according to the table above for all columns (which are factors) in the data frame?

PS So sorry if this seems like a trifle, but I searched for 2 days without an answer and tried all possible combinations. Perhaps I did not search enough enough = (

Thank you very much.

+6
source share
3 answers

You were almost there. Only one small change in your function would lead you there. x in function(x) ... must be passed 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-encoding of the code can make it even easier to read:

 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 
+8
source

Late, but here reshape2 possible solution. It could be very simple with recast , but we need to handle empty factor levels here, so we need to specify both factorsAsStrings = FALSE inside melt and drop = FALSE inside dcast , and recast cannot 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 did not care about empty levels, a quick solution would be simple

 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 with 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 
+7
source

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 me 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 
+5
source

Source: https://habr.com/ru/post/976543/


All Articles