Whcrs Whcrs - 1 month ago 9
R Question

Naming column and row names in R from another data type

I have an array like this:

> 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


and have another like this:

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

Answer

You can do something like

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

or

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