title: Outputting multiple arrays of data in R
body: <p>I have a code that loops through multiple subjects and outputs the run lengths of consecutive 1's in various arrays. The output is something like this:</p>
  
  <pre><code>Variable1RunLengths 2 3 14 12 7 8
  
  Variable2RunLengths 4 9 8 12 4 7 3
  </code></pre>
  
  <p>And it does this for multiple subjects. I know how to output single variable to a data frame, but I am having trouble outputting the arrays of data I'm calculating with this code. Any suggestions?</p>
  
  <pre><code>GetRL&lt;-function(df) {
  
   subjects &lt;- unique(df.all$Subject)
   numsubjects &lt;- length(subjects)
   runLengths.df &lt;- data.frame()
  
   for (i in 1:numsubjects) {
  
       subj &lt;- subjects[i]##names loop variable 
       subdf &lt;- df.all[which(df.all$Subject == subj),] ##pulls all data for current subject
       ## pulls vectors within current subject for each task
       patrmdf &lt;- subdf$Patient_Room
       compdf &lt;- subdf$comp
       pertoperdf &lt;- subdf$pertoper
       paperdf &lt;- subdf$paper
  
       ##calculates runs of ones for each task, pulls lengths or all values = 1
       patrmall &lt;- rle(patrmdf)
       patrmruns &lt;- patrmall$lengths[patrmall$values == 1]
       patrmslength &lt;- length(patrmruns)
       compall &lt;- rle(compdf)
       compruns &lt;- compall$lengths[compall$values == 1]
       complength &lt;- length(compruns)
       pertoperall &lt;- rle(pertoperdf)
       pertoperruns &lt;- pertoperall$lengths[pertoperall$values == 1]
       pertoperlength &lt;- length(pertoperruns)
       paperall &lt;- rle(paperdf)
       paperruns &lt;- paperall$lengths[paperall$values == 1]
       paperlength &lt;- length(paperruns)
  
  
       ##outputs vectors and variables
       runLengths.df &lt;- subj
       runLengths.df&lt;- patrmruns
       runLengths.df&lt;- compruns
       runLengths.df&lt;- pertoperruns
       runLengths.df &lt;- paperruns
   }
   return(runLengths.df)
  </code></pre>
  
  <p>}</p>