我正在使用《 R编程的艺术》一书学习R。在第6章中,作者Matloff使用了一个名为subtable <- function(tbl,subnames)的函数,但是当我键入此函数subtable时,它说没有找到该函数,然后我用谷歌搜索,发现它位于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’

我不明白,该如何使用此功能?有什么建议么?非常感激。

最佳答案

如果这本书有这样的东西

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控制台中编写(或复制粘贴)所有代码,便可以调用此函数。因此,我怀疑您是否要在此处安装任何新软件包!

08-16 23:48
查看更多