【问题标题】:Converting array (produced by "table") to data frame in r or s-plus将数组(由“表”生成)转换为 r 或 s-plus 中的数据框
【发布时间】:2012-11-02 20:31:11
【问题描述】:

我正在尝试将“table”函数生成的数组转换为函数内的普通数据框。我试过as.data.frame,得到is.data.frame=TRUE。但是,该对象仍然是一个带有额外暗淡的数组。当我将它与数据框合并时,这会导致问题,并且最终得到具有数组维度的数据框的单列。如何将对象强制转换为简单的数据框?额外的暗淡仅包含行名。我尝试将 rownames 设置为 null 无济于事。

【问题讨论】:

标签: arrays r


【解决方案1】:

如果您可以提供一个可重现的示例,将会有所帮助,但我之前也遇到过这个问题。有很多解决方案,但没有一个是完美的。

DF <- as.data.frame(as.matrix(table(whatev))) ## makes a data frame w/ one column
                                              ## and rownames = names(table)
DF$V2 <- rownames(DF)            ## or as.numeric(rownames(DF)) if you want
rownames(DF) <- NULL             ## no need for them anymore

还有

DF <- data.frame(V1 = as.vector(names(table(whatever))), V2 = as.numeric(table(w.e))

【讨论】:

    猜你喜欢
    • 2018-01-07
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多