将融化的 table 变回 table
Changing a melted table back into a table
泰坦尼克号是R中的一个数据集class是"table"。
dim(Titanic)
表示它是 4 维的。
现在,如果我这样做
x <- Titanic
y <- melt(x)
y 看起来像一个二维数据框。
我的问题是,如何将 y 改回 x?我能得到的最接近的是
z <- table(y)
但是 z 是 5 维的。
有人能帮帮我吗?
我找不到执行此操作的现有函数。但是,在使用 dput
查看 reshape2::melt(Titanic)
的输出后,我们可以使用其中的一部分并将正确的部分输入 reshape2::acast
到 "unmelt" 和 table:
library(reshape2)
unmelt_table <- function(melted_table, value.name = "value") {
premelt_dimnames <- names(melted_table)[names(melted_table) != value.name]
premelt_dimlabels <- sapply(premelt_dimnames, function(n)
levels(getElement(melted_table, n))
)
formula_colnames <- rev(premelt_dimnames)
recast_formula <- as.formula(paste0(". ~ ", paste(formula_colnames, collapse = " + ")))
premelt_vec <- as.vector(acast(melted_table, recast_formula))
structure(premelt_vec,
.Dim = as.vector(sapply(premelt_dimlabels, length)),
.Dimnames = premelt_dimlabels,
class = "table")
}
要验证它是否有效,只需 运行 这个:
> identical(Titanic, unmelt_table(melt(Titanic)))
[1] TRUE
关于 reshape2::melt(Titaninc)
结果的 melt
报道不多。与as.data.frame(Titanic)
的结果进行比较。唯一的区别是 'numbers' 列的名称(value
与 Freq
)。
通过执行 class(Titanic)
和 ?table
,您会发现 Titanic
是来自 table()
函数的数据类型。在文档中,您还可以注意到有 xtabs
,基于公式的制表界面。在 xtabs
文档中,formula
参数说
... On the left hand side, one may optionally give a vector or a matrix of counts; in the latter case, the columns are interpreted as corresponding to the levels of a variable. This is useful if the data have already been tabulated, ...
这给你留下了一个简单的
dt <- as.data.frame(Titanic)
xtabs( Freq ~ Class + Sex + Age + Survived, data = dt )
重构 table
分类数据,如果您需要它作为其他包的输入。
要补充我的两分钱,以简单的二维 data.frame
或更好的 tibble
来自 tidyverse
的形式使用交叉表更为可取!
泰坦尼克号是R中的一个数据集class是"table"。
dim(Titanic)
表示它是 4 维的。
现在,如果我这样做
x <- Titanic
y <- melt(x)
y 看起来像一个二维数据框。 我的问题是,如何将 y 改回 x?我能得到的最接近的是
z <- table(y)
但是 z 是 5 维的。
有人能帮帮我吗?
我找不到执行此操作的现有函数。但是,在使用 dput
查看 reshape2::melt(Titanic)
的输出后,我们可以使用其中的一部分并将正确的部分输入 reshape2::acast
到 "unmelt" 和 table:
library(reshape2)
unmelt_table <- function(melted_table, value.name = "value") {
premelt_dimnames <- names(melted_table)[names(melted_table) != value.name]
premelt_dimlabels <- sapply(premelt_dimnames, function(n)
levels(getElement(melted_table, n))
)
formula_colnames <- rev(premelt_dimnames)
recast_formula <- as.formula(paste0(". ~ ", paste(formula_colnames, collapse = " + ")))
premelt_vec <- as.vector(acast(melted_table, recast_formula))
structure(premelt_vec,
.Dim = as.vector(sapply(premelt_dimlabels, length)),
.Dimnames = premelt_dimlabels,
class = "table")
}
要验证它是否有效,只需 运行 这个:
> identical(Titanic, unmelt_table(melt(Titanic)))
[1] TRUE
关于 reshape2::melt(Titaninc)
结果的 melt
报道不多。与as.data.frame(Titanic)
的结果进行比较。唯一的区别是 'numbers' 列的名称(value
与 Freq
)。
通过执行 class(Titanic)
和 ?table
,您会发现 Titanic
是来自 table()
函数的数据类型。在文档中,您还可以注意到有 xtabs
,基于公式的制表界面。在 xtabs
文档中,formula
参数说
... On the left hand side, one may optionally give a vector or a matrix of counts; in the latter case, the columns are interpreted as corresponding to the levels of a variable. This is useful if the data have already been tabulated, ...
这给你留下了一个简单的
dt <- as.data.frame(Titanic)
xtabs( Freq ~ Class + Sex + Age + Survived, data = dt )
重构 table
分类数据,如果您需要它作为其他包的输入。
要补充我的两分钱,以简单的二维 data.frame
或更好的 tibble
来自 tidyverse
的形式使用交叉表更为可取!