且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用相邻值替换 R 向量中的 NA

更新时间:2023-02-18 11:20:22

你想要的似乎有两部分:

There seem to be 2 parts to what you want:

  1. 您想替换玩家名称 &具有预定价值观的团队
  2. 您希望通过playerGame列表结转游戏数量

对于(1),你可以:

df$team[is.na(df$team)] <- 'CRP' 

同样,您可以更改数据框的其他组件

Similarly you can alter the other component of the dataframe

对于 (2) 你可以这样做:

For (2) you could do this:

if(is.na(df$playerGame[1])) {
    df$playerGame[1] <- 0
}
for(i in 2:length(df$playerGame)) { 
    if(is.na(x[i])) {
        df$playerGame[i] <- df$playerGame[i-1]
    }
} 

那么 df$playerGame 是:

[1] 0 1 1 2 3 3

也许有一种非常漂亮的方法可以做到这一点,但这显然是可读的......

Perhaps there is a very nifty way to do this, but this is clearly readable...