html table (markov) changing names of states

I am trying to calculate how many (sum total) transitions happened between multiple states. I saw that this could be easily achieved by using html tables in Gmisc (open to other suggestions).

I have 2 columns called 'before' and 'after' and there are technically 23 states in total, with alphabetic character labels.

Code I used:
b4 <- b4 %>%
mutate_at(vars(1:1),
~as.numeric(recode(.,
"a"=1,
"b"=2,
"c"=3,
"d"=4,
"e"=5,
"f"=6,
"g"=7
"h"=8)))
b4 <- data.matrix(b4)

af <- af %>%
mutate_at(vars(1:1),
~as.numeric(recode(.,
"a"=1,
"b"=2,
"c"=3,
"d"=4,
"e"=5,
"f"=6,
"g"=7
"h"=8)))
af <- data.matrix(af)

b4 <- factor(b4)
af <- factor(af)

transition.mtrx <- table(b4,af)
htmlTable(transition.mtrx, title ' "transitions', ctable = TRUE)

The issue is that this only seems to accept numerical inputs (so I changed it as such). However; to get the labels to be correct on the final plot I need to manually type out
labels = c("a","b"......) when I know which transitions took place.

I was wondering if anyone knew how to say "look at the transition labels and change them back to the alphabetical original ones" or if there is a better way of doing this.

It sounds like you could just index into the LETTERS variable:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
tibble(nums = 1:3) %>%
  mutate(lets = LETTERS[nums])
#> # A tibble: 3 x 2
#>    nums lets 
#>   <int> <chr>
#> 1     1 A    
#> 2     2 B    
#> 3     3 C

Created on 2021-08-19 by the reprex package (v2.0.0)

Is this what you need?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.