0

我有以下内容:

print(strategy_reallocations)
                      1       13          17        
A                    "B"     "C"         "D"

print(dimnames(strategy_reallocations))
[[1]]
[1] "A"

[[2]]
[1] "B"  "C" "D"

print(is.character(strategy_reallocations))
[1] TRUE

我想向这个角色添加另一个元素,使其看起来像:

                  0       1       13       17      
A                "Z"     "B"     "C"      "D"

我在 G 上找不到正确的答案。到目前为止我尝试过的是,

strategy_reallocations <- c("Z", strategy_reallocations)

但这破坏了字符的数据结构。

print(strategy_reallocations)
[1] "Z"      "B"     "C" "D"

我怎样才能做到这一点?

编辑:

str(strategy_reallocations)
chr [1, 1:3] "B" "C" "D"
 - attr(*, "dimnames")=List of 2
  ..$ : chr "A"
  ..$ : chr [1:3] "1" "13" "17"
4

1 回答 1

4
x <- matrix(c("b", "c", "d"), nrow=1)
colnames(x) <- c(1, 13, 17)
rownames(x) <- "a"

cbind(`0`="z", x)
#  0   1   13  17 
#a "z" "b" "c" "d"

反引号是必需的,因为0它不是有效的变量名。

于 2013-11-13T12:07:05.570 回答