title: replace NA in an R vector with adjacent values
body: <p>I have a dataframe which has merged player and team data for soccer seasons
  So for a particular player in a specific season I have data like</p>
  
  <pre><code>df &lt;- data.frame(team=c(NA,"CRP",NA,"CRP","CRP",NA),
               player=c(NA,"Ed",NA,"Ed","Ed",NA),
               playerGame= c(NA,1,NA,2,3,NA),
               teamGame =c(1,2,3,4,5,6)) 
  </code></pre>
  
  <p>Where the NA's indicate that the player did not appear in that specific team game</p>
  
  <p>How would I most efficiently replace the team and player NA's with "CRP" and "Ed" respectively
  and have a plGame output of, in this instance,  0,1,1,2,3,3</p>
  
  <hr>
  
  <p><strong>EDIT</strong></p>
  
  <p>Sorry, I wrote this when I woke up in the middle of the night and may have over-simplified my problem too much. Only one person seems to have picked up on the fact that this is a subset of a much larger set of data and even he/she did not follow it though that a straight hardcode replacement of player and team was insufficient
   Thanks for the replies. Dsee's hint for the na.locf in the zoo package and the first line of AK's answer appears to offer the best way forward</p>
  
  <pre><code>df$playerGame[df$teamGame == min(df$teamGame) &amp; is.na(df$playerGame) == TRUE] &lt;- 0
  na.locf(df$playerGame)
  </code></pre>
  
  <p>This covers the eventuality of more than one NA to start the sequence. In my case the min(df$teamGame) will always be 1 so hardcoding that may speed things up</p>
  
  <p>A more realistic example is here</p>
  
  <pre><code>library(zoo)
  library(plyr)
  
  newdf &lt;- data.frame(team=c("CRP","CRP","CRP","CRP","CRP","CRP","TOT","TOT","TOT"),
               player=c(NA,"Ed",NA,"Bill","Bill",NA,NA,NA,"Tom"),
               playerGame= c(NA,1,NA,1,2,NA,NA,NA,1),
               teamGame =c(1,2,3,1,2,3,1,2,3))
  </code></pre>
  
  <p>I can now show the team for every row
  Each team plays three games in a season. Ed and Bill, play for CRP and appear in games 2 and 1,2 respectively. Tom plays for  TOT in game 3 only. Assume that player names are unique(even in real world data)</p>
  
  <p>It seems to me that  I need to create another column, 'playerTeam'</p>
  
  <pre><code>newdf$playerTeam &lt;- 0
  
  for (i in 1:nrow(newdf)) {
  newdf$playerTeam[i] &lt;-ceiling(i/3)
  }
  </code></pre>
  
  <p>I can then use this value to fill in the player gaps. I have used the sort functiom
  which omits NA</p>
  
  <pre><code>newdf &lt;- ddply(newdf,.(playerTeam),transform,player=sort(player)[1])
  </code></pre>
  
  <p>I can then use the aforementioned code</p>
  
  <pre><code>newdf$playerGame[newdf$teamGame == 1 &amp; is.na(newdf$playerGame) == TRUE] &lt;- 0
  newdf$playerGame &lt;- na.locf(newdf$playerGame)
  
     team player playerGame teamGame playerTeam
  1  CRP     Ed          0        1          1
  2  CRP     Ed          1        2          1
  3  CRP     Ed          1        3          1
  4  CRP   Bill          1        1          2
  5  CRP   Bill          2        2          2
  6  CRP   Bill          2        3          2
  7  TOT    Tom          0        1          3
  8  TOT    Tom          0        2          3
  9  TOT    Tom          1        3          3
  </code></pre>
  
  <p>I will need to build in season as well but that should not be a problem</p>
  
  <p>Am I missing anything here?</p>
  
  <p>I have several hundred thousand rows to process so any speed ups would be helpful. For instance I would probably want to avoid ddply and use a data.table approach or another apply function, right</p>