[R] Finding combination of states

Eric Berger er|cjberger @end|ng |rom gm@||@com
Mon Sep 4 16:17:26 CEST 2023


The function purrr::cross() can help you with this. For example:

f <- function(states, nsteps, first, last) {
   paste(first, unlist(lapply(purrr::cross(rep(list(v),nsteps-2)),
\(x) paste(unlist(x), collapse=""))), last, sep="")
}
f(LETTERS[1:5], 3, "B", "E")
[1] "BAE" "BBE" "BCE" "BDE" "BEE"

HTH,
Eric


On Mon, Sep 4, 2023 at 3:42 PM Christofer Bogaso
<bogaso.christofer using gmail.com> wrote:
>
> Let say I have 3 time points.as T0, T1, and T2.(number of such time
> points can be arbitrary) In each time point, an object can be any of 5
> states, A, B, C, D, E (number of such states can be arbitrary)
>
> I need to find all possible ways, how that object starting with state
> B (say) at time T0, can be on state E (example) in time T2
>
> For example one possibility is BAE etc.
>
> Is there any function available with R, that can give me a vector of
> such possibilities for arbitrary number of states, time, and for a
> given initial and final (desired) states?
>
> ANy pointer will be very appreciated.
>
> Thanks for your time.
>
> ______________________________________________
> R-help using 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.



More information about the R-help mailing list