从其他数据类型命名列和行名中的R [英] naming column and row names in r from another data type

查看:106
本文介绍了从其他数据类型命名列和行名中的R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的数组:

> 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"

问题很简单:如何创建与这些行和列名空矩阵或表(无所谓)

Question is simple: How can I create an empty matrix or table (doesn't matter) with these row and column names?

推荐答案

您可以不喜欢

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))

这篇关于从其他数据类型命名列和行名中的R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆