我想将数据帧转换为仅数字矩阵。我的目标:
num [1:3, 1:4] 1 2 3 1 2 3 1 2 3 1 ...
就这样。
alpha <- beta <- gamma <- delta <- c(1,2,3)
df <- data.frame(alpha, beta, gamma, delta, stringsAsFactors = FALSE)
M1 <- as.matrix(df, ncol = ncol(df))
str(M1)
M2 <- data.matrix(df)
str(M2)
num [1:3, 1:4] 1 2 3 1 2 3 1 2 3 1 ... - attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:4] "alpha" "beta" "gamma" "delta"
如果没有所有属性,我一辈子都无法弄清楚如何做到这一点。我需要纯形式作为另一种方法的输入。
最佳答案
@thelatemail 是绝对正确的。
unname(as.matrix(df
诀窍。
关于r - 如何将数据框转换为没有列名的矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56050389/