title: Return value from function when iterating in a loop R
body: <p>I have stayed away from functions in R but decided it is better practice. Now I have this issue. I write my function:</p>
  
  <pre><code> myFunction&lt;-function(tab){ #takes tabular input
       inP&lt;-c()
       for (x in 1:dim(tab)[1]){ #iterate over rows
           rname&lt;-rownames(tab)[x] #rownames as output
           if(rname&lt;5) inP&lt;-c(inP,rname) #trivial work
       }
       return(inP) #return the vector made above
   }
  
   tablist&lt;-as.list(paste("tab",1:4,sep=""))
   for (x in 1:length(tablist)){
       tablist[[x]]&lt;-table(c(1:10),c(1:10))
   }
  
   inPvec&lt;-c() #predefine vector to concatenate results into
   for (x in 1:length(tablist)){ #tabs holds multiple tables as a list
        myFunction(tablist[[x]]) #run myFunction for each table held in tabs
        inPvec&lt;-c(inPvec,inP) #concatenate results from each iteration
   }
  
   inP
   #NULL
  
   myFunction(tablist[[1]])
   #[1] "1" "2" "3" "4" "10"
  </code></pre>
  
  <p>Edited as workable example: apologies for laziness.</p>
  
  <p>If you run for the loop in the example, inP returns NULL, as does inPvec. Bu running single tables in the function return the correct value. However, inP is NULL on calling it, so I guess this is where my issue is.</p>
  
  <p>I want everything from the loop iterating over the function to be returned to a single vector eg:</p>
  
  <pre><code> inPVec
   #[1] "1" "2" "3" "4" "10" "1" "2" "3" "4" "10" "1" "2" "3" "4" "10" etc
  </code></pre>
  
  <p>Any help much appreciated.</p>