[R] Finding combination of states

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Mon Sep 4 21:31:07 CEST 2023


I think there may be some uncertainty here about what the OP requested. My
interpretation is:

n different times
k different states
Any state can appear at any time in the vector of times and can be repeated
Initial and final states are given

So modifying Tim's expand.grid() solution a bit yields:

g <- function(ntimes, states, init, final){
   ## ntimes: integer, number of unique times
   ## states: vector of unique states
   ## init: initial state
   ## final: final state
do.call(paste0,c(init,expand.grid(rep(list(states), ntimes-2)), final))
}

e.g.

> g(4, LETTERS[1:5], "B", "D")
 [1] "BAAD" "BBAD" "BCAD" "BDAD" "BEAD" "BABD" "BBBD" "BCBD"
 [9] "BDBD" "BEBD" "BACD" "BBCD" "BCCD" "BDCD" "BECD" "BADD"
[17] "BBDD" "BCDD" "BDDD" "BEDD" "BAED" "BBED" "BCED" "BDED"
[25] "BEED"

If neither this nor any of the other suggestions is not what is desired, I
think the OP will have to clarify his query.

Cheers,
Bert

On Mon, Sep 4, 2023 at 9:25 AM Ebert,Timothy Aaron <tebert using ufl.edu> wrote:

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

	[[alternative HTML version deleted]]



More information about the R-help mailing list