【问题标题】:From contingency tables to data.frame in R从列联表到 R 中的 data.frame
【发布时间】:2016-05-15 08:27:36
【问题描述】:

我的出发点是有几个包含我从文本中提取的 POS 标签的字符向量。例如:

c("NNS", "VBP", "JJ",  "CC",  "DT")
c("NNS", "PRP", "JJ",  "RB",  "VB")

我使用table()ftable() 来计算每个标签的出现次数。

 CC  DT  JJ NNS VBP 
 1   1   1   1   1

最终目标是让 data.frame 看起来像这样:

   NNS VBP PRP JJ CC RB DT VB
1  1   1   0   1  1  0  1  0
2  1   0   1   1  0  1  0  1 

在这里使用plyr::rbind.fill 对我来说似乎是合理的,但它需要 data.frame 对象作为输入。但是,当使用as.data.frame.matrix(table(POS_vector)) 时会发生错误。

Error in seq_len(ncols) : 
argument must be coercible to non-negative integer

使用as.data.frame.matrix(ftable(POS_vector)) 实际上会生成一个data.frame,但没有colnames。

V1 V2 V3 V4 V5 ...
1  1  1  1  1

非常感谢任何帮助。

【问题讨论】:

    标签: r dataframe contingency


    【解决方案1】:

    在base R中,你可以试试:

    table(rev(stack(setNames(dat, seq_along(dat)))))
    

    您也可以使用“qdapTools”中的mtabulate

    library(qdapTools)
    mtabulate(dat)
    #   CC DT JJ NNS PRP RB VB VBP
    # 1  1  1  1   1   0  0  0   1
    # 2  0  0  1   1   1  1  1   0
    

    dat 与@Heroka 的回答中定义的相同:

    dat <- list(c("NNS", "VBP", "JJ",  "CC",  "DT"),
                c("NNS", "PRP", "JJ",  "RB",  "VB"))
    

    【讨论】:

    • 完美运行!感谢你们的快速帮助。
    【解决方案2】:

    这可能是一种解决方法,但这可能是一个解决方案。

    我们假设我们所有的向量都在一个列表中:

    dat <- list(c("NNS", "VBP", "JJ",  "CC",  "DT"),
    c("NNS", "PRP", "JJ",  "RB",  "VB"))
    

    然后我们将表格转换为转置矩阵,然后我们将其转换为 data.table:

    library(data.table)
    temp <- lapply(dat,function(x){
      data.table(t(as.matrix(table(x))))
    })
    

    然后我们使用rbindlist 来创建所需的输出:

    rbindlist(temp,fill=T)
    

    我们也可以选择先把我们所有的数据放在一个data.table中,然后再进行聚合。请注意,这假设向量长度相等。

    temp <- as.data.table(dat)
    #turn to long format
    temp_m <- melt(temp, measure.vars=colnames(temp))
    
    #count values for each variable/value-combination, then reshape to wide
    res <- dcast(temp_m[,.N,by=.(variable,value)], variable~value,value.var="N", fill=0)
    

    【讨论】:

    • 非常优雅。您会考虑将此添加到您的答案中吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 2017-01-08
    • 1970-01-01
    • 2017-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多