规范化列联表

时间:2013-06-07 18:07:26

标签: r contingency

假设我有一张桌子

> tab <- table(c("www.a.com","a.com","a.com","www.b.com"))
> tab
    a.com www.a.com www.b.com 
        2         1         1 

我想合并"www.a.com""a.com"的条目以获取

    a.com  b.com 
        3      1

我有

> fqdn.normalization.regexp <- "^ww*[0-9]*\\."
> normalize.fqdn <- function (domain) sub(fqdn.normalization.regexp,"",domain)
> normalize.fqdn(names(tab))
[1] "a.com" "a.com" "b.com"

问题是,如何将tab值转换为我需要的值?

1 个答案:

答案 0 :(得分:0)

> names(tab) <- normalize.fqdn(names(tab))
> tapply(tab, names(tab), sum)
a.com b.com 
    3     1