title: how to loop through columns and then add them to a dataframe
body: <p>So the problem that I am working on requires me to go through a large data frame of actors/actresses and create a profile for them based on the movies that they have already acted in. I created this profile function which can be used for a single actor/actress, but I am having trouble looping it through the entire data frame to get a profile for all of the actors/actresses.    </p>
  
  <pre><code>profile &lt;- function(actor, df) {
     dftest &lt;- subset(df, df$id.x == actor)
     fit &lt;- rpart(name.y ~ id.x, method = "class", dftest)
     p &lt;- predict(fit, dftest)
     return(colnames(p)[max.col(p,ties.method="first")][1])
    }
  </code></pre>
  
  <p>Here is the for loop that I have already, but I keep getting errors and am unsure if I am going about this method correctly. </p>
  
  <pre><code>for (k in c(1)) {
    user.frame &lt;- data.frame()
    for (i in df2$id.x) { # df2$id.x is the column of actors names
      user.frame[i] &lt;- data.frame(profile(i, df2))
    }
    df2final &lt;- rbind(final, user.frame)
    View(df2fin)
  }
  </code></pre>
  
  <p>** Edit ** </p>
  
  <p>This is the data that is going into the profile function</p>
  
  <pre><code># A tibble: 278,361 x 7
     movie_id title   id.x name.x            id.y   id1 name.y         
        &lt;int&gt; &lt;chr&gt;  &lt;int&gt; &lt;chr&gt;            &lt;int&gt; &lt;int&gt; &lt;chr&gt;          
   1    19995 Avatar 65731 Sam Worthington  19995    28 Action         
   2    19995 Avatar 65731 Sam Worthington  19995    12 Adventure      
   3    19995 Avatar 65731 Sam Worthington  19995    14 Fantasy        
   4    19995 Avatar 65731 Sam Worthington  19995   878 Science Fiction
   5    19995 Avatar  8691 Zoe Saldana      19995    28 Action         
   6    19995 Avatar  8691 Zoe Saldana      19995    12 Adventure      
   7    19995 Avatar  8691 Zoe Saldana      19995    14 Fantasy        
   8    19995 Avatar  8691 Zoe Saldana      19995   878 Science Fiction
   9    19995 Avatar 10205 Sigourney Weaver 19995    28 Action         
  10    19995 Avatar 10205 Sigourney Weaver 19995    12 Adventure    
  </code></pre>
  
  <p>Ideally, I would like the for loop to give me a data frame at the end that has the actor ID in one column and the profile genre next to it. The error I keep getting is this </p>
  
  <pre><code>Error in `[&lt;-.data.frame`(`*tmp*`, i, value = list(profile.i..df2. = 1L)) : 
    new columns would leave holes after existing columns 
  </code></pre>
  
  <p>So i used your code and did this:</p>
  
  <pre><code>df &lt;- data.frame(a = rep(df2$id.x))
  df$b &lt;- df2$name.y
  
  actor.names &lt;- unique(df$a)
  result.file &lt;- matrix(ncol = 2, nrow = length(actor.names))
  
  for(i in 1:length(actor.names)){
  
    dftest  &lt;- subset(df, a == actor.names[i])    #subset actor name
    fit     &lt;- rpart::rpart(b ~ a, method = "class", dftest) #run model
    p       &lt;- predict(fit, dftest) #predict genre
    temp    &lt;- colnames(p)[max.col(p,ties.method="first")][1]
  
    result.file[i,1] &lt;- actor.names[i]
    result.file[i,2] &lt;- temp
  }
  </code></pre>
  
  <p>It gave me this error:</p>
  
  <pre><code>Error in cbind(yval2, yprob, nodeprob) : 
    number of rows of matrices must match (see arg 2)
  </code></pre>
  
  <p>But the results that I got were what i needed. Should i be worried?</p>
  
  <pre><code>result.file
       [,1]      [,2]             
  [1,] "65731"   "Action"         
  [2,] "8691"    "Action"         
  [3,] "10205"   "Comedy"         
  [4,] "32747"   "Action"         
  [5,] "17647"   "Action"         
  [6,] "1771"    "Drama"          
  [7,] "59231"   "Comedy"         
  [8,] "30485"   "Action"         
  [9,] "15853"   "Adventure"      
  </code></pre>
  
  <p>[10,] "10964"   "Drama"     </p>
  
  <p>Here is the dput(head(df))</p>
  
  <pre><code>dput(head(df))
  structure(list(a = c(65731L, 65731L, 65731L, 65731L, 8691L, 8691L
  ), b = c("Action", "Adventure", "Fantasy", "Science Fiction", 
  "Action", "Adventure")), .Names = c("a", "b"), row.names = c(NA, 
  6L), class = "data.frame")     
  </code></pre>