PigWolf PigWolf - 2 months ago 8
R Question

Rename inherited names in a matrix in R

I have a data frame called noNAN which has a list of 21 columns [class 0....class 20] which I have converted to a table of proportions for each row and saved this as 'percentagePrach'

percentagePrach <- round(prop.table(as.matrix(noNAN[,9:29]),1) *100, 1)


I wish to rename the inherited column labels for percentagePrach to the following structure [ % class 0, % class 1........ % class 20]

I've tried the following code but I am getting an error relating to the length of 'dimnames' not being equal to the array extent??

dimnames(percentagePrach) <- list(colnames(percentagePrach, do.NULL = FALSE, prefix = "% class "))

Error in dimnames(percentagePrach) <- list(colnames(percentagePrach, do.NULL = FALSE, :
length of 'dimnames' [1] not equal to array extent


Could anyone elaborate on what array this is referring to?

Could I specify the column names in the original prop.table as an argument?

and and
Answer

Could I specify the column names in the original prop.table as an argument?

exactly there is the function colnames()

colnames(percentagePrach) <- c("newcolname1", "newcolname2", "newcolname3", ...)