[R] Finding combination of states

Ebert,Timothy Aaron tebert @end|ng |rom u||@edu
Mon Sep 4 18:24:39 CEST 2023


Does this work for you?

t0<-t1<-t2<-LETTERS[1:5]
al2<-expand.grid(t0, t1, t2)
al3<-paste(al2$Var1, al2$Var2, al2$Var3)
al4 <- gsub(" ", "", al3)
head(al3)

Tim

-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Eric Berger
Sent: Monday, September 4, 2023 10:17 AM
To: Christofer Bogaso <bogaso.christofer using gmail.com>
Cc: r-help <r-help using r-project.org>
Subject: Re: [R] Finding combination of states

[External Email]

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%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl.edu
> %7C25cee5ce26a8423daaa508dbad51c402%7C0d4da0f84a314d76ace60a62331e1b84
> %7C0%7C0%7C638294338934034595%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAw
> MDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sda
> ta=TM4jGF39Gy3PH0T3nnQpT%2BLogkVxifv%2Fudv9hWPwbss%3D&reserved=0
> PLEASE do read the posting guide
> http://www.r/
> -project.org%2Fposting-guide.html&data=05%7C01%7Ctebert%40ufl.edu%7C25
> cee5ce26a8423daaa508dbad51c402%7C0d4da0f84a314d76ace60a62331e1b84%7C0%
> 7C0%7C638294338934034595%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiL
> CJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=5n
> PTLmsz0lOz47t41u578t9oI0i7BOgIX53yx8CesLs%3D&reserved=0
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
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