Replacing NA`s with 0 or space depending on the position of the `NA` in a string
<p>I have a data frame with a million of long strings which contains 0, 1, NA`s.</p>

<p>I have to replace NA`s based on the following method:</p>

<ol>
<li>all of the NA at the end of the string has to be replaced with space</li>
<li>all of the NA at the middle of the string has to be changed to 0.</li>
</ol>

<p>Example:
Let`s assume I have the following string
0011NANA01NA0011NANANANA</p>

<p>My desired output:
'011000100011____', 
which means at the end of the string all of the NA should be replaced by space (I used '_' to indicate spaces).</p>

<p>AFAIK I should use gsub() to make this changes. I tried to use the following code`s without any success.</p>

<p><code>gsub("NA", " ", "0011NANA01NA0011NANANANA")</code> -  which replaces all of the NAs with space.
<code>gsub("NA$", " ", "0011NANA01NA0011NANANANA")</code> - which replaces the last NA of the string with space.</p>

<p>This works fine if I have only one NA at the end of the string. But how can I change all of the 4 NA`s at the end of the string in this example?
Could someone help me out with this problem?
Thanks in advance for all kind of help!</p>