title: Whitespace string can't be replaced with NA in R
body: <p>I want to substitute whitespaces with NA. A simple way could be <code>df[df == ""] &lt;- NA</code>, and that works for most of the cells of my data frame....but not for everyone!</p>
  
  <p>I have the following code:</p>
  
  <pre><code>library(rvest)
  library(dplyr)
  library(tidyr)
  
  #Read website
  htmlpage &lt;- read_html("http://www.soccervista.com/results-Liga_MX_Apertura-2016_2017-844815.html")
  
  #Extract table
  df &lt;- htmlpage %&gt;% html_nodes("table") %&gt;% html_table()
  df &lt;- as.data.frame(df)
  
  #Set whitespaces into NA's
  df[df == ""] &lt;- NA
  </code></pre>
  
  <p>I figured out that some whitespaces have a little whitespace between the quotation marks</p>
  
  <p><code>df[11,1]
  [1] " "</code></p>
  
  <p>So my solution was to do the next: <code>df[df == " "] &lt;- NA</code></p>
  
  <p>However the problem is still there and it has the little whitespace! I thought the trim function would work but it didn't...</p>
  
  <pre><code>#Trim
  df[,c(1:10)] &lt;- sapply(df[,c(1:10)], trimws)
  </code></pre>
  
  <p>However, the problem can't go off.</p>
  
  <p>Any ideas?</p>