0

I am using the gmisc package to make plots like the following one.Transition Diagrams

The gmisc package provides the function getTransitions transitions$getTransitionSet(1)

that returns a table with the transitions. For example

      7 87 94 130 185 188 192 199
  6   1  0  0   0   0   0   0   0
  79  0  0  1   0   0   0   0   0
  87  0  1  0   0   0   0   0   0
  130 0  0  0   3   0   0   0   0
  185 0  0  0   0   1   0   0   0

The numbers in the table now contain the frequency that something happened. For example from state 130 3 times it has moved to state 130. I would like to change those numbers into percentages.

I am not sure what is the best way that will respect the dplyr tables. What I see with str is that

str(transitions$getTransitionSet(1))
 'table' int [1:8, 1:8] 1 0 0 0 0 0 0 0 0 0 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:8] "6" "79" "87" "130" ...
  ..$ : chr [1:8] "7" "87" "94" "130" ...

Is there a nice way that per row will find out the percentage of each value?

Thanks in advance Alex

Alex P
  • 137
  • 1
  • 13
  • If you have a matrix, and you want to normalize the values rowwise, do it by: `t(apply(mydata, 1, function(x)(x-min(x))/(max(x)-min(x))))` as shown at [this link](https://stackoverflow.com/questions/20046257/normalize-rows-of-a-matrix-within-range-0-and-1) . Although, you have to multiply the values by 100 to get the percentages. – Newl May 07 '19 at 12:08
  • I wanted to say thanks for your reply. Unfortunately I can not vote yet. Btw What should be the best data type to convert my matrix to do things like : "Which are the largest X values in the table, return me their indexes". Should I keep it as a table? – Alex P May 08 '19 at 13:18
  • Since you have the same datatype in the rows and columns, you can continue to work on tables, or maybe transform it to a matrix, and set row and colnames if the cast won't do it. You can index it with the correct logical statements that you mentioned before e.g: `M <- as.matrix(transitions); which(M == max(M), arr.ind = T)` . This snippet shows you the index of the largest value's index. – Newl May 08 '19 at 19:43

0 Answers0