replace NA in an R vector with adjacent values

Posted by pssguy on Stack Overflow See other posts from Stack Overflow or by pssguy
Published on 2012-11-22T13:57:28Z Indexed on 2012/11/22 17:00 UTC
Read the original article Hit count: 206

Filed under:
|
|

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

df <- 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)) 

Where the NA's indicate that the player did not appear in that specific team game

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

© Stack Overflow or respective owner

Related posts about r

    Related posts about vector