次のようなデータフレームがあるとします。
>df
city  year  ceep
  1    1      1
  1    2      1
  1    3      0
  1    4      1
  1    5      0
  2    1      0
  2    2      1
  2    3      1
  2    4      0
  2    5      1
  3    1      1
  3    2      0
  3    3      1
  3    4      0
  3    5      1
次に、異なる行の「city」と「ceep」の値に依存する新しい変数「veep」を作成します。例えば、
veep=1 if ceep[_n-1]=1 & city=city[_n-1]
veep=1 if ceep[_n+2]=1 & ceep[_n+3]=1 & city=city[_n+3] 
n観測の行はどこにありますか。これらの条件をR言語に翻訳する方法がわかりません。私が問題を抱えているのは、観測の列を選択することだと思います。私は次の行に沿ったどこかのコードを考えています:
df$veep[df$ceep(of the n-1th observation)==1 & city==city(n-1th observ.)] <- 1
df$veep[df$ceep(of the n+2th observation)==1 & df$ceep(of the n+3th observation)==1 &
city==city(n+3th observ.)] <- 1
#note: what's in parentheses is just to demonstrate where I'm having trouble 
誰かがこれについて助けを提供できますか?