title: looping through each element in a list and add number to each item. using R
body: <p>I read in a file and R returns a list, like this:<br>
  list 1:
  "1)"      "Seo  "   "agad "   "na "     "ciad "   "faclan " "a "<br>
  list 2:  "cannteil " "(canntail) "     "a-staigh "       "dhan "       "<br>
  list 3: "2)"       "Seo "     "sinn, "   "sin "     "direach " "fuirich…"  </p>
  
  <p>What I want is to get a vector where, if the 1st element in [[i]] has a number, then the other elements in [[i]] also get the same number and if the 1st element in [[i]] doesn't have a number, then all elements in [[i]] will have the number shown in the previous line, like this:  </p>
  
  <p>"1\t1)"       "1\tSeo  "    "1\tagad "    "1\tna "  "1\tciad "    "1\tfaclan "  "1\ta "       ....
  "2\t2)" "2\tseo", 2\tsinn....</p>
  
  <p>Could anyone tell me the code for this? And, is there a way to get a vector containing only the number corresponding each word without pasting it before each word?</p>
  
  <p>Thank you</p>
  
  <p>My code was the following, but it didn't give me what I want (all the elements get the number 1, even those in the list starting with number 2.)
  What part of the code is wrong?</p>
  
  <pre><code>word="" 
  temp="" 
  for (i in 1:length(file)) { 
         if (grepl('\\d+\)',file[[i]][1])) {       
         snum=grep('\\d+',file[[i]][1]) 
         temp=paste(snum, file[[i]], sep="\t") 
           } else { 
         temp=paste(snum,utter.short[[i]],sep="\t") 
           }   word=c(word,temp) 
       }
  </code></pre>