如何将数据帧转换为没有列名的矩阵

How to convert dataframe to matrix without column names

提问人:Robert Hadow 提问时间:5/9/2019 更新时间:10/8/2021 访问量:3290

问:

我想将数据帧转换为纯数字矩阵。我的目标:

数字 [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)

数字 [1:3, 1:4] 1 2 3 1 2 3 1 2 3 1 ... - attr(*, “dimnames”)=2
个列表 ..$ : 空
..$ : chr [1:4] “alpha” “beta” “gamma” “delta”

我一辈子都无法弄清楚如何在没有所有属性的情况下做到这一点。我需要纯形式作为另一种方法的输入。

R 矩阵

评论

4赞 thelatemail 5/9/2019
unname(as.matrix(df))?
1赞 IRTFM 5/9/2019
dimnames(M1) <- NULL.

答:

8赞 Robert Hadow 5/9/2019 #1

@thelatemail是绝对正确的。

unname(as.matrix(df))

做诀窍。