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 at 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 at 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.