1

我有以下矩阵:

> dat

       [,1]      [,2]       [,3]        [,4]
foo 0.7574657 0.2104075 0.02922241 0.002705617
foo 0.0000000 0.0000000 0.00000000 0.000000000
foo 0.0000000 0.0000000 0.00000000 0.000000000
foo 0.0000000 0.0000000 0.00000000 0.000000000
foo 0.0000000 0.0000000 0.00000000 0.000000000
foo 0.0000000 0.0000000 0.00000000 0.000000000

并给出这个:

> new_names <- c("f0","f1","f2","f3","f4","f5");
# where the length of new_names matches the number of rows in the dat matrix

我怎样才能得到这个?

        [,1]      [,2]       [,3]        [,4]
f0 0.7574657 0.2104075 0.02922241 0.002705617
f1 0.0000000 0.0000000 0.00000000 0.000000000
f2 0.0000000 0.0000000 0.00000000 0.000000000
f3 0.0000000 0.0000000 0.00000000 0.000000000
f4 0.0000000 0.0000000 0.00000000 0.000000000
f5 0.0000000 0.0000000 0.00000000 0.000000000
4

2 回答 2

4

也许是这样的?

dat <- matrix(c(c(0.7574657, 0.2104075, 0.02922241, 0.002705617), rep(0, 4*6-4)), ncol = 4, nrow = 6, byrow = TRUE)

rownames(dat) <- paste0("f", seq(0, nrow(dat)-1))

如果您已经有一个名称向量,那么

rownames(dat) <- new_names
于 2012-05-15T09:44:26.433 回答
0

试试这个:

行名(dat) <- c("f0","f1","f2","f3","f4","f5")

于 2018-03-04T20:40:33.797 回答