[R] "Improvement with the R code"
Berry, Charles
ccberry at ucsd.edu
Mon Aug 28 20:52:08 CEST 2017
> On Aug 28, 2017, at 9:26 AM, Elie Canonici Merle <elie.canonicimerle at gmail.com> wrote:
>
> Chuck (Is it fine to call you Chuck?)
In this forum, yes please.
> I don't know much about pmin and factor but it might worth looking into if
> you want to manipulate states by names (I assume this is why one might want
> to use it?)
>
Actually is it because the OP had states 1-4 in his data. In the pre-state only 1-3 get counted. In the post-state, 4 gets rolled into 3.
So,
pre <- factor(data, 1:3) # 4 and higher are NA
post <- pmin(3, data) # 4 and higher become 3
implements those rules, and table(pre, post) gives a 3 x 3 table that one need not subset.
Chuck
> generate_transition_matrix <- function(data, states)
> prop.table(table(head(data, -1), tail(data, -1)), 1)[states,]
>
>
> checkdf=data.frame(clusterNum=c(3,2,3,1,1,3,1,3,2,1,1,3,2,1,3,2))
>
> states=c(2,3)
>
> transition_matrix= generate_transition_matrix(checkdf$clusterNum, states)
> transition_matrix
>
More information about the R-help
mailing list