title: Change variable in a column with a for loop using R
body: <p>In my data frame, I have a column named <code>Localisation</code>. I want to change the data that is stored in it.</p>
  <p>Thalamus, External capsule or Lenticulate for 0,
  Cerebellum or Brain stem for 2 and
  Frontal, Occipital, Parietal or Temporal for 1</p>
  <p>I’m trying to do a For loop for this operation. My for statement doesn’t seem to be right, because I received</p>
  <pre><code>Error in for (. in i) 15:nrow(Localisation) : 
    4 arguments passed to 'for' which requires 3
  </code></pre>
  <p>and I don’t know how to compose my if statement.</p>
  <pre><code>dfM &lt;- dfM %&gt;%
    for(i in dfM$Localisation) if(i = &quot;Thalamus&quot;| &quot;External capsule&quot;| &quot;Lenticulate&quot;){
      dfM$Localisation &lt;- &quot;0&quot;
    } else if ( i = &quot;Cerebellum&quot;| &quot;Brain stem&quot;) {
      dfM$Localisation &lt;- &quot;2&quot;
    } else {
      dfM$Localisation &lt;- &quot;1&quot;
    }
  </code></pre>
  <p>I know similar questions have been asked multiple times, but I can’t find a way to work with my data.</p>
  <pre><code>dput(head(dfM))
  structure(list(new_id = c(&quot;5&quot;, &quot;9&quot;, &quot;10&quot;, &quot;16&quot;, &quot;30&quot;, &quot;31&quot;), 
      Localisation = c(&quot;Frontal&quot;, &quot;Thalamus&quot;, &quot;Occipital &quot;, &quot;Frontal&quot;, 
      &quot;External capsule&quot;, &quot;Cerebellum&quot;), HIV.CT.initial = c(0L, 
      1L, 1L, 1L, 0L, 0L), Anticoagulant = c(&quot;Warfarin&quot;, &quot;DOAC&quot;, 
      &quot;Warfarin&quot;, &quot;Warfarin&quot;, &quot;Warfarin&quot;, &quot;Warfarin&quot;), Sex = c(1L, 
      1L, 1L, 2L, 2L, 1L), HTA = c(1L, 1L, 1L, 1L, 1L, 0L), Systolic_BP = c(116L, 
      169L, 164L, 109L, 134L, 146L), Diastolic_BP = c(70L, 65L, 
      80L, 60L, 75L, 85L), ACO = c(1L, 0L, 1L, 1L, 1L, 1L), Type.NACO = c(NA, 
      2L, NA, NA, NA, NA), Dose.ACO = c(NA, NA, NA, NA, NA, NA), 
      APT = c(0L, 0L, 1L, 0L, 1L, 0L), INR = c(4.2, 1.1, 1.9, 1.3, 
      3.6, 2.8), GLW = c(13L, 15L, 14L, 14L, 15L, 15L), GLW.Prog = c(NA, 
      NA, NA, NA, 14L, NA), mRS = c(4L, 3L, 4L, 6L, 6L, 5L), Time.to.scan = c(NA, 
      NA, NA, NA, NA, NA), Chx = c(0L, 0L, 0L, 0L, 0L, 0L), Type.Chx = c(NA_real_, 
      NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), Date.Chx = c(&quot;&quot;, 
      &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;), décès.hospit = c(0L, 0L, 0L, 1L, 1L, 
      0L), décès.HIP = c(NA, NA, NA, 2L, 1L, NA), X3.mois = c(NA, 
      NA, NA, NA, NA, 1L), X6.mois = c(NA_integer_, NA_integer_, 
      NA_integer_, NA_integer_, NA_integer_, NA_integer_), X12.mois = c(NA_integer_, 
      NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
      ), X3.mois.1 = c(NA, NA, NA, 1L, 1L, 1L), X6.mois.1 = c(NA, 
      NA, NA, 1L, 1L, 1L), X12.mois.1 = c(NA, NA, NA, 1L, 1L, 1L
      ), X.1.y = c(NA, NA, NA, NA, NA, NA), ID = c(5L, 9L, 10L, 
      16L, 30L, 31L)), row.names = c(NA, 6L), class = &quot;data.frame&quot;)
  </code></pre>