2
votes

I'm learning R using the book, The Art of R Programming. And in chapter 6, the author Matloff used a function called subtable <- function(tbl,subnames), but as I typed this function subtable, it says not find the function, and I googled, find it's in the package extracat, so I installed this package, but as I'm loading this package, library(extracat), an error message came out, saying

library(extracat)
   Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) : 
   there is no package called ‘plyr’
   Error: package or namespace load failed for ‘extracat’

I can't understand this, how should I use this function? Any suggestions? Much appreciated.

1
this seems to mean that you need to install the plyr package in order to use the extracat package. So just write install.packages("plyr") and then run: library("extracat") - marbel

1 Answers

8
votes

If the book has something like this

subtable <- function(tbl,subnames)

the author is defining a new function called subtable, that receives two parameters called tbl and subnames. The code right below that is what the function runs. I found here and here the function you seem to refer to:

subtable <- function(tbl,subnames) {
   # get array of cell counts in tbl
   tblarray <- unclass(tbl)  
   # we'll get the subarray of cell counts corresponding to subnames by
   # calling do.call() on the "[" function; we need to build up a list
   # of arguments first
   dcargs <- list(tblarray)  
   ndims <- length(subnames)  # number of dimensions 
   for (i in 1:ndims) {
      dcargs[[i+1]] <- subnames[[i]]
   }  
   subarray <- do.call("[",dcargs)  
   # now we'll build the new table, consisting of the subarray, the
   # numbers of levels in each dimension, and the dimnames() value, plus
   # the "table" class attribute
   dims <- lapply(subnames,length) 
   subtbl <- array(subarray,dims,dimnames=subnames)
   class(subtbl) <- "table"
   return(subtbl)
}

Once you write (or copy-paste) all that code in your R console, you'll be able to call this function. So I doubt you want to install any new packages here!