0

I am trying to build a large table in R. Yes I have heard of the table() function - in fact, I've used it several times in the code below - but I am building this because I do not want to type table() 20 times a day. I plan on just exporting this using xtable + knitr. The reason this is useful is that for those of us who have to repeatedly tabulate data, this would save a lot of time. Unfortunately, there is something wrong with the loop down here:

ESRD <- rep(c("Y", "N"), each=10)
DIABETES <- rep(c("Y", "N", "Y", "N"), c(5, 5, 5, 5))
BLAH <- rep(c("Y", "N"), each=10)
categoricalvariables <- data.frame(ESRD, DIABETES, BLAH)

descriptives <- function(VARIABLEMATRIX){
desc <- matrix(0, ncol=4, nrow=2*ncol(VARIABLEMATRIX) + ncol(VARIABLEMATRIX))
  for (i in 1:ncol(VARIABLEMATRIX)){
    matper <- matrix(0, nrow=dim(table(VARIABLEMATRIX[ ,i])), ncol=1)
    for (i in 1:dim(table(VARIABLEMATRIX[ ,i]))){
      matper[i, ] <- paste(round(prop.table(table(VARIABLEMATRIX[ ,i]))[i]*100, 2), "%")
    }

    matcount <- matrix(0, nrow=dim(table(VARIABLEMATRIX[ ,i])), ncol=1)
    for (i in 1:dim(table(VARIABLEMATRIX[ ,i]))){
      matcount[i, ] <- table(VARIABLEMATRIX[ ,i])[i]
    }

    desc[((3*i)-2), ] <- c(colnames(VARIABLEMATRIX)[i], "", "", "") 
    desc[((3*i)-1):(3*i), ] <- cbind("", names(table(VARIABLEMATRIX[ ,i])), matcount[ ,1], matper[ ,1])
return(desc)
}
}
descriptives(categoricalvariables)

The output I am getting is (clearly there is a bug but I am not sure what is wrong):

     [,1]       [,2] [,3] [,4]  
 [1,] "0"        "0"  "0"  "0"   
 [2,] "0"        "0"  "0"  "0"   
 [3,] "0"        "0"  "0"  "0"   
 [4,] "DIABETES" ""   ""   ""    
 [5,] ""         "N"  "10" "50 %"
 [6,] ""         "Y"  "10" "50 %"
 [7,] "0"        "0"  "0"  "0"   
 [8,] "0"        "0"  "0"  "0"   
 [9,] "0"        "0"  "0"  "0"  

The expected output should be:

     [,1]       [,2] [,3] [,4]  
 [1,] "ESRD"     ""   ""   ""     
 [2,] ""         "N"  "10" "50 %" 
 [3,] ""         "Y"  "10" "50 %"   
 [4,] "DIABETES" ""   ""   ""    
 [5,] ""         "N"  "10" "50 %"
 [6,] ""         "Y"  "10" "50 %"
 [7,] "BLAH"     ""   ""   ""     
 [8,] ""         "N"  "10" "50 %"  
 [9,] ""         "Y"  "10" "50 %"
  • How does the expected output differ from that of `with(categoricalvariables, table(ESRD, DIABETES, BLAH))`? – Ernest A Mar 09 '17 at 23:47
  • I am not cross tabulating them. I want to get a large table with just descriptives for each variable. That's how they're different. – user3810226 Mar 10 '17 at 02:14

1 Answers1

0

Here's one option:

desc <- function(x) {
    af <- table(x)
    rf <- prop.table(af) * 100
    out <- cbind(Absolute=af, `Relative(%)`=rf)
    dimnames(out) <- setNames(dimnames(out), c('Values', 'Frequency'))
    out
}

lapply(categoricalvariables, desc)
#$ESRD
#      Frequency
#Values Absolute Relative(%)
#     N       10          50
#     Y       10          50
#
#$DIABETES
#      Frequency
#Values Absolute Relative(%)
#     N       10          50
#     Y       10          50
#
#$BLAH
#      Frequency
#Values Absolute Relative(%)
#     N       10          50
#     Y       10          50

If you really want a character matrix

tmp <- lapply(categoricalvariables, desc)

out <- do.call(rbind, lapply(names(tmp), function(x) {
    rbind(c(x, "", "", ""), cbind("", rownames(tmp[[x]]), tmp[[x]]))
}))
out <- unname(rbind(c("", "", "Abs.Freq", "Rel.Freq"), out))
out
#      [,1]       [,2] [,3]       [,4]      
# [1,] ""         ""   "Abs.Freq" "Rel.Freq"
# [2,] "ESRD"     ""   ""         ""        
# [3,] ""         "N"  "10"       "50"      
# [4,] ""         "Y"  "10"       "50"      
# [5,] "DIABETES" ""   ""         ""        
# [6,] ""         "N"  "10"       "50"      
# [7,] ""         "Y"  "10"       "50"      
# [8,] "BLAH"     ""   ""         ""        
# [9,] ""         "N"  "10"       "50"      
#[10,] ""         "Y"  "10"       "50"      
Ernest A
  • 7,526
  • 8
  • 34
  • 40