在R中命名另一种数据类型的列和行名称

时间:2016-04-17 20:23:51

标签: arrays r dataframe

我有一个这样的数组:

> columnnames
[1] trade    grain    crude    corn     ship     wheat    acq      interest money.fx earn
> summary(columnnames)
 acq     corn    crude     earn    grain interest money.fx     ship    trade    wheat 
   1        1        1        1        1        1        1        1        1        1 
> str(columnnames)
Factor w/ 10 levels "acq","corn","crude",..: 9 5 3 2 8 10 1 6 7 4

并有另外一个这样的:

> rownames
 [1] "tp" "fn" "fp" "tn" "tpr" "fnr"  "precision" "recall"  "acc"  "Fmeasure"

问题很简单:如何使用这些行名和列名创建空矩阵或表(无关紧要)?

1 个答案:

答案 0 :(得分:1)

您可以执行类似

的操作
your.matrix <- matrix(NA, nrow = length(rownames), ncol = length(columnnames))
rownames(your.matrix) <- rownames
colnames(your.matrix) <- as.character(columnnames)

your.matrix <- matrix(NA, nrow = length(rownames), ncol = length(columnnames), dimnames = list(rownames, columnnames))