Quantcast
Channel: R: Introducing a WHILE LOOP to a Function - Stack Overflow
Viewing all articles
Browse latest Browse all 2

R: Introducing a WHILE LOOP to a Function

$
0
0

I am working with the R programming language.

I am trying to count the first time a certain pattern (e.g. ABCD) appears in a random string (e.g. ACABCDCDBCABCDBC - answer =6 ). I wrote a function to do this:

library(stringr)letters <- c("A", "B", "C", "D")results <- list()for (i in 1:100){    iteration_i = i    letters_i = paste(sample(letters, 100, replace=TRUE, prob=c(0.25, 0.25, 0.25, 0.25)),collapse="")    position_i = str_locate(letters_i, "ADBC")     results_tmp = data.frame(iteration_i , letters_i, position_i)    results[[i]] <- results_tmp}results_df <- do.call(rbind.data.frame, results)

This looks something like this now (note: I don't think this is correct - in row 5, I see ABCD at the beginning of the row, but its being recorded as NA for some reason):

  iteration_i                                                                                            letters_i start end1           1 BACDCCCDCCCDCDDBBCBBAACACBBBBAAABDDDACAABDDABBABADCDDCDACCBBBCABCDABCDCCCDADDDBADBDCADAABDBDCDCAACCB    NA  NA2           2 CACACCCCDCCBADACBBAADBCABBAAAAADBDDBCADCAAADADAAABDCABBAABABBCBDADCDDDDCDBADDBDCBCDDDBDCDDAACBBBBACA    20  233           3 CDCBDAABDDDDADBAAABBADAADBDDDBDADDCABADDDCDABBBCBCBBACBBDADABBCDCCACDBCDCDDBDBADBCDCADDADDDBDBAAABBD    79  824           4 ADBCDBADADBAAACAADACACACACBDDCACBDACCBDAAABDBAAAABBCCDBADADDADCBCABCBAABDCBCDCDACDCCDBADCBDDAADBCDAC     1   45           5 D**ABCD**DDCCBCDABADBBBBCDBCADCBBBDCAAACACCCBCBCADBDDABBACACBDABAAACCAAAAACCCCBCBCCABABDDADBABDDDCCDDCCC    NA  NA6           6 DDDDDBDDDDBDDDABDDADAADCABCDAABBCCCDAABDDAACBDABBBBBABBCBDADBDCCAAADACCBCDDBDCAADCBBBCACDBBADDDDCABC    NA  NA

Currently, I am only generating 100 letters and hoping that this is enough to observe the desired pattern (sometimes this doesn't happen, notice the NA's) - is there a way to add a WHILE LOOP to what I have written to keep generating letters until the desired pattern first appears?

Can someone please show me how to do this?

Thanks!


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images