[prev in list] [next in list] [prev in thread] [next in thread] 

List:       r-help
Subject:    Re: [R] "Improvement with the R code"
From:       "Berry, Charles" <ccberry () ucsd ! edu>
Date:       2017-08-28 18:52:08
Message-ID: 54017153-F096-470F-820D-273961AC92EC () ucsd ! edu
[Download RAW message or body]


> On Aug 28, 2017, at 9:26 AM, Elie Canonici Merle <elie.canonicimerle@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
> 

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic