r - How to change name of row variable in a table -


it's easy change names of rows (e.g., rownames()), that's not i'm after. consider:

> newtab <- xtabs(~as.factor(letters[1:2])+letters[1:2]) > newtab                        letters[1:2] as.factor(letters[1:2]) b                       1 0                       b 0 1 

i want this:

          upper case lower case b          1 0          b 0 1 

but if try:

> dimnames(newtab) <- list("lower case", "upper case") 

i error:

error in dimnames(newtab) <- list("lower case", "upper case") :
length of 'dimnames' [1] not equal array extent

look @ output of str(newtab):

> str(newtab)  xtabs [1:2, 1:2] 1 0 0 1  - attr(*, "dimnames")=list of 2   ..$ as.factor(letters[1:2]): chr [1:2] "a" "b"   ..$ letters[1:2]           : chr [1:2] "a" "b"  - attr(*, "class")= chr [1:2] "xtabs" "table"  - attr(*, "call")= language xtabs(formula = ~as.factor(letters[1:2]) + letters[1:2]) 

as.factor(letters[1:2]) , letters[1:2] names of dimnames list. want set names of dimnames list, not dimnames themselves. can via like:

> dimnames(newtab) <- setnames(dimnames(newtab),c("lower case", "upper case")) > # or > names(dimnames(newtab)) <- c("lower case", "upper case") > newtab           upper case lower case b          1 0          b 0 1 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -