无法找到功能子表格

时间:2013-12-25 23:44:25

标签: r

我正在学习R使用“The Programming of R Programming”这本书。在第6章中,作者Matloff使用了一个名为subtable <- function(tbl,subnames)的函数,但是当我输入这个函数subtable时,它说找不到函数,我用Google搜索,发现它在包extracat中所以我安装了这个软件包,但是当我加载这个软件包library(extracat)时,出现了一条错误消息,说

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’

我无法理解这一点,我应该如何使用这个功能?有什么建议?非常感谢。

1 个答案:

答案 0 :(得分:8)

如果这本书有这样的东西

subtable <- function(tbl,subnames)

作者定义名为subtable的新功能,它接收两个名为tblsubnames的参数。下面的代码是函数运行的代码。我找到了herehere您似乎参考的功能:

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)
}

在R控制台中编写(或复制粘贴)所有代码后,您将能够调用此函数。所以我怀疑你想在这里安装任何新的包裹!