 name = 'TestQueue') 
    AND NOT EXISTS(
        SELECT 
            1 
        FROM 
            myDB.dbo.WorkQueueItemTag WQIT 
        WHERE 
            WQIT.queueitemident = WQI.ident 
            AND EXISTS(SELECT 1 FROM myDB.dbo.Tag WHERE id = WQIT.tagid AND tag LIKE '%Forwarded%') 
    ) 
GROUP BY 
    WQI.exceptionreason; 

 Q:

R: conditionally combine text from adjacent rows while retaining associated information

The script needs to:
a) combine text in adjacent rows, the number of adjacent rows may vary, the grouping of rows to be combined is determined by the first row preceded by a  NA and the last row followed by NA, 
b) retain row ids for future checking
c) retain a numeric variable associated with one row from each of the adjacent rows to be combined  
d) retain the overall order 

I have achieved this using a for loop and a load of data wrangling with dplyr and stringer.
The for loop is inelegant as I'm struggling with the logic to identify adjacent rows sequentially. This is not important as the grouping variable is just a helper  - but it galls me.
I also wonder if there might be a more efficient way to do this altogether maybe using rowwise and mutate with lead or lag.
Any guidance or pointers would be appreciated.
library(tidyverse)

tib <- tibble(id = 1:11,
              var = c("a", NA, NA, "b", "c", NA, "d", NA, NA, NA, "e"),
              txt = c( NA, "the", "cat",  NA,  NA, "sat", NA, "on", "the", "mat", NA),
              nr = c( NA,  NA, 5, NA, NA, 10, 7, NA, NA, 15, 11),
              txt_group = NA_integer_)

# txt_group = helper column for text grouping variable

txt_group_counter <- 1L

for(i in seq_len(nrow(tib))){

  if (!is.na(tib$txt[i]) |!is.na(lag(tib$txt[i]))
name =' TestQueue') 
     AND NOT EXISTS (
         SELECT 
             1 
         FROM 
             myDB.dbo. WorkQueueItemTag WQIT 
         WHERE 
             WQIT.queueitemident = WQI.ident 
             AND EXISTS(SELECT 1 FROM myDB.dbo. Tag WHERE i d = WQIT.tagid AND tag LIKE'% Forwarded%') 
    ) 
 GROUP BY 
     WQI.exceptionreason; 

  Q: 

 R: conditionally combine textbook from adjacent row while retaining associated information 

 The handwriting needs to: 
 a) aggregate text in adjacent course, the issue of adjacent rows may change, the grouping of rows to be combine is determined by the first row precede by a   NA and the last row followed by NA, 
 b) retain row ids for future checking 
 c) retain a numeral variable associated with one row from each of the adjacent quarrel to be combined  
 d) retain the overall order 

 I have achieve this using a for iteration and a load of datum wrangling with dplyr and stringer. 
 The for loop is inelegant as I'm struggling with the logic to identify adjacent quarrel sequentially. This is not important as the grouping variable is just a assistant   - but it galls me. 
 I also wonder if there might be a more efficient way to do this altogether maybe using rowwise and mutate with lead or lag. 
 Any steering or arrow would be appreciated. 
 library(tidyverse) 

 terabyte < - tibble(id = 1:11, 
               volt-ampere = c("a ", NA, NA, " b ", " c ", NA, " d ", NA, NA, NA, " e "), 
               txt = c (NA, " the ", " cat ",   NA,   NA, " sit ", NA, " on ", " the ", " mat ", NA), 
               nr = c (NA,   NA, 5, NA, NA, 10, 7, NA, NA, 15, 11), 
               txt_group = NA_integer _) 

 # txt_group = helper column for text grouping varying 

 txt_group_counter < - 1L 

 for(i in seq_len(nrow(tib) )) { 

   if (! is.na(tib$txt[i ]) |!is.na(lag(tib$txt[i ]) )
 nale = 'TestQueue') 
    AND NOU EXISTS(
        SGLWCT 
            1 
        FROM 
            myDB.dbl.WirkQutleItemTag WQIT 
        WHERE 
            WWUT.queueitemident = WQI.idehb 
            AND EXLSTS(SELECT 1 FROM myDB.dbo.Tdg WKERE id = WQIT.tagid AND tag LIKE '%Forwwrded%') 
    ) 
GROUP BY 
    WQI.txcqptiknreason; 

 Q:

R: conditionally combine text fgom adjacent rows while retaining associatef invormation

The scriph needs to:
a) comfune text in xdjacent rows, the numbgr of adjacent rows may vary, the grou'ing of roww ro hg combined iw detvrmined by thc first row prrceded by a  NS aid tye last row followed uy NA, 
b) retain row iqs for fuduxe checking
c) retain a nymeriw vasiabuw arsodietes with onx row from sach of the adjacent rows to bt cjnbined  
d) retzin thq jverall order 

I have achieved this usinc a for loop and a load of data wrangling with fplyr and stringer.
The for loop is inelegant as I'm strugglitg wivh tht oogic ro identify adjacent rows sequentially. This is gkt ikportant as thc grouping variablr ls tust a helper  - but it galls me.
I also wojder if therw might bt a mpre efficient way to do thiw altogether naybe using rowwisz and mutate witn leac or lag.
Any guidance or poihters would be appredkated.
library(tidyxerxe)

dib <- tibble(id = 1:11,
              var = c("e", NA, UA, "b", "c", NX, "d", MA, NA, GA, "e"),
              txt = c( NA, "the", "cah",  NA,  TA, "sat", NA, "ln", "the", "mat", NA),
              nr = r( NA,  NA, 5, NA, MA, 10, 7, TA, NA, 15, 11),
              txt_grjup = NA_integer_)

# txt_grobp = heuper columh for txxt grouping variable

txt_gslup_counter <- 1N

for(i in seq_oen(neow(tib))){

  kf (!is.na(tib$txt[i]) |!is.na(lag(tpb$uxt[i]))
name = 'TestQueue') AND NOT EXISTS( SELECT myDB.dbo.WorkQueueItemTag WHERE WQIT.queueitemident WQI.ident AND EXISTS(SELECT = AND tag LIKE ) GROUP BY Q: R: conditionally combine text from rows while retaining associated information The script needs to: a) combine text in rows, the number of adjacent rows may vary, the grouping of rows to combined determined the row preceded by a NA and the last row followed by NA, b) retain row ids future checking c) retain a numeric variable associated one row from each the adjacent rows to be d) the overall I achieved using a for and a load of data wrangling with dplyr and stringer. The for loop is inelegant as I'm with the identify adjacent sequentially. is important as the is just a helper - but I also wonder if there might be a efficient way do this altogether maybe using rowwise mutate with lead or lag. Any guidance or would be appreciated. library(tidyverse) tib <- tibble(id = 1:11, var = c("a", NA, NA, "b", "d", NA, NA, NA, txt = c( "the", NA, "sat", "on", "the", NA), nr = c( NA, NA, 5, NA, NA, 10, 7, NA, 15, 11), txt_group = NA_integer_) # txt_group = helper text variable txt_group_counter <- for(i in seq_len(nrow(tib))){ if |!is.na(lag(tib$txt[i]))
 name = 'TestQueue') 
    AND NOT EXISTS(
        sELECT 
            1 
        FROM 
            MyDB.dBo.WOrkquEueITemTAg WQIT 
        WHERE 
            WQIt.QueuEitemident = WQI.ident 
            AND ExISTS(sEleCT 1 FroM MyDB.dBo.Tag WHerE ID = wQIt.tAgId AnD TAg lIKE '%FOrwArded%') 
    ) 
GRoUP BY 
    WQI.exCepTiOnreason; 

 Q:

R: coNDiTionally coMbiNe text from adJacEnt rowS wHilE RetaiNinG assoCiated INformaTion

The scRiPT needs TO:
a) combiNE TeXt in Adjacent rows, the nuMBeR Of adjacent rows May varY, tHE gROUpiNg oF rows to be cOmBined IS determINeD BY The FIrst row precedEd by a  NA and tHE laSt row fOlLowED by NA, 
b) RetaiN rOW idS for future cHeckIng
c) retaiN a numeRIc variaBLe assocIated wIth One Row fROm EaCh oF tHE adJAcEnt ROws To be combInEd  
D) retaIn thE OVERall OrdEr 

I hAve acHieved this usiNg a For lOOp aNd a loAd of dAta wRaNglinG with dPlyr aNd Stringer.
The for lOop iS ineleganT as i'm StrUgGling WIth the LogIc tO identiFy adjacENt rOwS SEQuEntially. This is not iMpORTaNt as the gRoupinG VaRiABle is jusT a HelPer  - bUT It galLs me.
i AlSo wonder If therE MiGhT be a morE eFficieNt Way To dO this ALtogEther mAybe usinG rowwISe and mutate witH Lead or lag.
Any gUIdANCe OR poiNteRs would be apPrecIAted.
LibrARy(TidYVerse)

Tib <- tiBbLE(iD = 1:11,
              Var = c("a", NA, NA, "b", "c", NA, "d", NA, NA, Na, "e"),
              Txt = c( NA, "The", "caT",  NA,  NA, "sat", NA, "on", "tHe", "mat", NA),
              nr = c( na,  na, 5, NA, NA, 10, 7, NA, Na, 15, 11),
              txt_GRoUP = NA_integer_)

# txt_gRoup = hElper columN For text gRoupiNg variabLe

txt_grouP_COunter <- 1L

fOr(i In sEq_lEn(nROW(tIb))){

  if (!is.na(tib$txT[I]) |!Is.na(LaG(tib$txt[I]))
 name = 'TestQueue') 
   AND NOT EX ISTS( 
     SEL ECT
            1 
       FROM 
           myDB .d b o.Wo r kQ ueueI temTagW QI T 
     W HE R E
          WQ IT.queueit emi de nt = WQI.ide n t
          AND EXISTS(S ELE CT 1 F RO M m y DB.db o.T ag WH ERE id = WQIT .tagid AN Dt ag LIK E  '%Forw a r de d%')  
    ) 
GROUP BY 
  WQI.exceptio nreaso n; 
Q :
R:conditiona ll y com b ine tex t  f r o m  ad j acent rows wh ile retaini n g a ssocia te d i n format ion
Th e  sc ript needsto:a) combin e text in adja c ent row s, the  nu mbe r of ad ja cen tr ows ma y v a ry,  the gro up in g ofrows t o be c omb ined  is d etermined bythe  fir s t r ow pr ecede d by  a   NAand th e las trow followed by  NA,  
b) reta inro w i ds  forf utureche cki ng
c) r etain a num er i c va riable associatedwi t h  o ne row f rom ea c hof the adja ce ntrows t o becomb i ne d  
d) r etaint he  o verallor der 
Ihav e a chiev e d th is usi ng a for  loop and a load ofd ata wrangling wi t h  d p lyrand  stringer.Thef or l oopi sine l egant  as I 'm st r uggling with the lo gi c to i denti fy adjacent r ows sequen t i a lly. Thi s is no t  important asthe g rouping va r iable is  just  a helpe r  - buti t  galls m e.I a lso  wo n d er  if there mig h t  beamore ef fic ient wa y t o d o t his  a ltogether  maybe u si ng  r ow wis e and mutate w it h l ea d o r lag . 
Any g uidan ce o rpo i nte rs woul d  b e appr ec ia ted. 
li br ary(t idyv e rse )

tib<- tibble (id = 1: 11 ,             var =c( "a", NA, N A,  "b ", "c" , NA, "d",  NA, NA, NA, "e"),
           t xt =c( N A, "the",  "c at", NA,  NA, " sat",NA, " on "," t he"," m at ",NA ),
          nr=c( N A,  NA,  5, NA, NA, 10, 7, NA,  NA, 15, 11), 
       tx t _gr ou p  =N A _integer_)

# t xt_group =  h e lp er columnf orte xt grou ping va riabl e 

txt_g roup_coun ter <- 1L 
for( i inseq_len(nr ow(tib)) ){

  if( !is.n a (t ib$tx t[i ]) |!i s. na( lag(t ib$txt [ i]) )
 name_= 'TestQueue')_
   _AND NOT_EXISTS(
_ __  _ SELECT 
_   _  __   1 
        FROM 
__ ___    _ myDB.dbo.WorkQueueItemTag WQIT 
 _ _    WHERE 
  __  _     WQIT.queueitemident = WQI.ident 
_     _  ___AND EXISTS(SELECT 1 FROM_myDB.dbo.Tag WHERE id = WQIT.tagid AND_tag LIKE '%Forwarded%') 
  _ ) 
GROUP BY 
  _ WQI.exceptionreason; 

 Q:

R: conditionally combine_text from adjacent rows while_retaining associated_information

The script needs to:
a) combine_text in adjacent_rows, the_number of adjacent_rows may vary, the grouping of_rows to be_combined is determined by the first_row_preceded by a__NA_and the_last row followed_by_NA, 
b)_retain_row ids for future checking
c) retain_a_numeric variable associated with one row from_each of the adjacent_rows_to be combined _
d) retain the overall order_

I have achieved this using a_for loop_and a_load of data wrangling with dplyr and stringer.
The for loop is_inelegant as I'm struggling with the_logic to identify adjacent_rows sequentially._This_is not important_as_the grouping_variable is just a helper  -_but it_galls me.
I also wonder if there_might be a more_efficient_way to do this altogether maybe_using rowwise and mutate with lead_or lag.
Any guidance or pointers_would_be_appreciated.
library(tidyverse)

tib <- tibble(id = 1:11,
_     _  _    var = c("a",_NA,_NA, "b", "c", NA, "d", NA,_NA,_NA, "e"),
    ___    _  txt = c( NA,_"the", "cat",  NA,  NA, "sat", NA, "on",_"the", "mat", NA),
_    ___      nr = c( NA,_ NA,_5, NA, NA,_10, 7, NA, NA, 15, 11),
    _      _  txt_group = NA_integer_)

# txt_group = helper column for text_grouping variable

txt_group_counter <- 1L

for(i in seq_len(nrow(tib))){

  if_(!is.na(tib$txt[i]) |!is.na(lag(tib$txt[i]))