找不到R函数row_sums和col_sums的文档

时间:2013-02-24 19:22:22

标签: r tm

我正在尝试解决有关更改tm包中的tf-idf加权函数的问题:https://stackoverflow.com/questions/15045313/changing-tf-idf-weight-function-weight-not-by-occurrences-of-term-but-by-numbe

这样做,我正在查看weightTfIdf函数,其中包含m上的以下代码,一个TermDocumentMatrix。

cs <- col_sums(m)

rs <- row_sums(m)

但我找不到任何有关函数row_sumscol_sums的文档;当我尝试使用它们编写自己的加权函数时,我收到一个错误:Error in weighting(x) : could not find function "col_sums"

这些功能在哪里定义?

我已粘贴以下R的完整功能信息:

function (m, normalize = TRUE) 
{
    isDTM <- inherits(m, "DocumentTermMatrix")
    if (isDTM) 
        m <- t(m)
    if (normalize) {
        cs <- col_sums(m)
        if (any(cs == 0)) 
            warning("empty document(s): ", paste(Docs(m)[cs == 
                0], collapse = " "))
        names(cs) <- seq_len(nDocs(m))
        m$v <- m$v/cs[m$j]
    }
    rs <- row_sums(m > 0)
    if (any(rs == 0)) 
        warning("unreferenced term(s): ", paste(Terms(m)[rs == 
            0], collapse = " "))
    lnrs <- log2(nDocs(m)/rs)
    lnrs[!is.finite(lnrs)] <- 0
    m <- m * lnrs
    attr(m, "Weighting") <- c(sprintf("%s%s", "term frequency - inverse document frequency", 
        if (normalize) " (normalized)" else ""), "tf-idf")
    if (isDTM) 
        t(m)
    else m
}
<environment: namespace:tm>
attr(,"class")
[1] "WeightFunction" "function"      
attr(,"Name")
[1] "term frequency - inverse document frequency"
attr(,"Acronym")
[1] "tf-idf"

1 个答案:

答案 0 :(得分:15)

您正在寻找的功能位于slam包中。由于slam仅导入且不是依赖项,因此查看文档需要一些工作。这是一个关于如何解决问题并查看文档的示例会话。

> # I'm assuming you loaded tm first
> library(tm)
> # See if we can view the code
> col_sums
Error: object 'col_sums' not found
> # Use getAnywhere to grab the function even if the function is 
> # in a namespace that isn't exported
> getAnywhere("col_sums")
A single object matching ‘col_sums’ was found
It was found in the following places
  namespace:slam
with value

function (x, na.rm = FALSE, dims = 1, ...) 
UseMethod("col_sums")
<environment: namespace:slam>
> # So the function is in the slam package
> slam::col_sums
function (x, na.rm = FALSE, dims = 1, ...) 
UseMethod("col_sums")
<environment: namespace:slam>
> # We can tell help to look in the slam package now that we know
> # where the function is from    
> help(col_sums, package = "slam")
> # alternatively
> library(slam)
> ?col_sums
> # If we want to view the actual code for col_sums we need to 
> # do a little work too
> methods("col_sums")
[1] col_sums.default*               col_sums.simple_triplet_matrix*

   Non-visible functions are asterisked
> # We probably want the default version?  Otherwise change to the other one
> getAnywhere("col_sums.default")
A single object matching ‘col_sums.default’ was found
It was found in the following places
  registered S3 method for col_sums from namespace slam
  namespace:slam
with value

function (x, na.rm = FALSE, dims = 1, ...) 
base:::colSums(x, na.rm, dims, ...)
<environment: namespace:slam>

因此col_sums函数只是基函数colSums的包装器。