title: Failing to create a column and change its values in a for loop
body: <p>I'm using a dataframe about movies that is somewhat structured like this:</p>
  <pre><code>movie_name      genres
      A       Comedy|Romance
      B       Action|Crime|Thriller
      C       Comedy|Musical|Western
  </code></pre>
  <p>I'm trying to create some kind of one hot encoding on the genres column by each individual genre
  here's what i managed so far</p>
  <pre><code># extract the unique genres from the genre column
  gens &lt;- str_split(movie_data$genres,'\\|', simplify = TRUE)
  remove &lt;- c('')                             # remove empty spaces that appear for some reason
  gens &lt;- gens[! gens %in% remove()]          # read above
  gens &lt;- unique(gens)                        # keep the unique genres
  
  gens now like this
  [1] &quot;Comedy&quot;  &quot;Romance&quot; &quot;Action&quot;  &quot;Crime&quot;  &quot;Thriller&quot;
  [2] &quot;Musical&quot;  &quot;Western&quot;
  </code></pre>
  <p>So far so good, here is also what i managed to do with the one hot encoding</p>
  <pre><code># this is specific for the Comedy Genre
  movie_data$Is_Comedy &lt;- integer(nrow(movie_data)) # create an empty column filled with values of 0
  
  movie_data$Is_Comedy[which(grepl(gens[1],as.matrix(movie_data$genres)))] &lt;- 1
   # turn all 0 values into 1 if the string &quot;Comedy&quot; is in the genres, gens[1] would be &quot;Comedy&quot; in this case
   # i turned the data into a matrix for faster execution
  </code></pre>
  <p>The code above works fine, but i am failing in trying to fit it in a loop of some kind for all the genres so i'm forced to write it once for each genre.
  Here is the code i'm trying to figure out</p>
  <pre><code>for (x in seq(1, length(gens), by = 1)){
    movie_data[,gens[x]] &lt;- integer(nrow(movie_data))
    movie_data[,gens[x]][which(grepl(gens[x],as.matrix(movie_data$gens)))] &lt;- 1
  }
  
  prints out this:
  Supplied 9000053 items to be assigned to 9000055 items of column 'Comedy'. If you wish to 'recycle' the RHS please use rep() to make this intent clear to readers of your code.
  </code></pre>
  <p>The error message is what i'm getting on my current data that i'm messing with, thanks in advance.</p>