【发布时间】:2020-11-26 16:35:25
【问题描述】:
我想将数据框转换为矩阵。
我成功使用了reshape2 包中的acast 函数,但想知道如何在base R 中执行此操作。
# Create data
set.seed(123)
df <- tidyr::expand_grid(x = c(1,2,3), y = c(0,-0.5,-1))
df <- cbind(df, z=rnorm(9))
# Convert dataframe to matrix
example_matrix <- reshape2::acast(df, x~y, value.var="z")
问题
reshape2 的 acast 函数的基本 R 替代方案是什么?
【问题讨论】: