title: For loop over columns in R
body: <p>I would like to iterate over columns in dataframe and for every column if the number of NAs is bigger than 50% of all entries I would like to remove that column from the dataframe. So far I have something like this but it doesn't work:</p>
  
  <pre><code>for (i in names(df_r)) {
      if (sum(is.na(df_r[,i]))/length(df_r) &gt; 0.5) {
          df_r &lt;- df_r[, -i]
          }
      }
  </code></pre>
  
  <p>I am more of a python guy and I am learning R so I might be mixing syntax here.</p>