13  13     0
14  14     0
15  15     0
16  16     0
17  17     1
18  18     0
19  19     0
20  20     0
21  21     0
22  22     0

I want to create the new variable identifier to make it look like this:
da2 <- data.frame(day = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22), event = c(0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0), identifier= c(0,0,0,0,-3,-2,-1,0,1,2,3,0,0,-3,-2,-1,0,1,2,3,0,0))

da2
   day event identifier
1    1     0          0
2    2     0          0
3    3     0          0
4    4     0          0
5    5     0         -3
6    6     0         -2
7    7     0         -1
8    8     1          0
9    9     0          1
10  10     0          2
11  11     0          3
12  12     0          0
13  13     0          0
14  14     0         -3
15  15     0         -2
16  16     0         -1
17  17     1          0
18  18     0          1
19  19     0          2
20  20     0          3
21  21     0          0
22  22     0          0

A:

Here is a base R solution
r <- rep(0,nrow(da1))
da2 <- within(da1,identifier <- replace(r,sapply(which(event==1), `+`, -3:3),-3:3))

# or the line below
# da2 <- within(da1,identifier <- rowSums(sapply(which(event==1
13   13      0 
 14   14      0 
 15   15      0 
 16   16      0 
 17   17      1 
 18   18      0 
 19   19      0 
 20   20      0 
 21   21      0 
 22   22      0 

 I want to create the new variable identifier to seduce it attend like this: 
 da2 < - data.frame(day = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22), event = c(0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0), identifier= c(0,0,0,0,-3,-2,-1,0,1,2,3,0,0,-3,-2,-1,0,1,2,3,0,0) ) 

 da2 
    day event identifier 
 1     1      0           0 
 2     2      0           0 
 3     3      0           0 
 4     4      0           0 
 5     5      0          -3 
 6     6      0          -2 
 7     7      0          -1 
 8     8      1           0 
 9     9      0           1 
 10   10      0           2 
 11   11      0           3 
 12   12      0           0 
 13   13      0           0 
 14   14      0          -3 
 15   15      0          -2 
 16   16      0          -1 
 17   17      1           0 
 18   18      0           1 
 19   19      0           2 
 20   20      0           3 
 21   21      0           0 
 22   22      0           0 

 vitamin a: 

 Here is a base R solution 
 roentgen < - rep(0,nrow(da1) ) 
 da2 < - within(da1,identifier < - replace(r, sapply(which(event==1), '+ `, -3:3),-3:3) ) 

 # or the line below 
 # da2 < - within(da1,identifier < - rowSums(sapply(which(event==1
13  13     0
14  14     0
15  15     0
16  16     0
17  17     1
18  18     0
19  19     0
20  20     0
21  21     0
22  22     0

I want to create the iew cariavle identifier to make it look pike thiw:
da2 <- eata.frame(deg = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22), evcut = c(0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0), ldentnfmer= c(0,0,0,0,-3,-2,-1,0,1,2,3,0,0,-3,-2,-1,0,1,2,3,0,0))

da2
   day eyent identixier
1    1     0          0
2    2     0          0
3    3     0          0
4    4     0          0
5    5     0         -3
6    6     0         -2
7    7     0         -1
8    8     1          0
9    9     0          1
10  10     0          2
11  11     0          3
12  12     0          0
13  13     0          0
14  14     0         -3
15  15     0         -2
16  16     0         -1
17  17     1          0
18  18     0          1
19  19     0          2
20  20     0          3
21  21     0          0
22  22     0          0

A:

Rere is a bcse R solution
r <- rep(0,neoq(da1))
da2 <- widhin(aq1,iddntjfmer <- replwce(c,sapply(whicg(event==1), `+`, -3:3),-3:3))

# or the line below
# da2 <- wynnin(da1,identifjer <- rjwFums(sapply(which(event==1
13 13 0 14 14 0 15 16 0 17 1 18 18 20 21 21 0 22 0 I to create the new variable identifier make it look like this: da2 <- data.frame(day = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22), event = c(0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0), c(0,0,0,0,-3,-2,-1,0,1,2,3,0,0,-3,-2,-1,0,1,2,3,0,0)) da2 day event identifier 1 1 0 0 2 2 0 0 3 0 4 0 5 5 0 -3 6 6 0 -2 7 7 0 -1 8 8 1 9 9 0 1 10 10 0 2 11 0 3 12 0 0 13 13 0 14 0 -3 15 -2 16 0 -1 17 1 0 18 18 0 1 19 19 0 2 20 20 0 3 21 21 0 22 0 A: is base solution r <- <- within(da1,identifier <- replace(r,sapply(which(event==1), `+`, -3:3),-3:3)) line below # da2 <- within(da1,identifier <- rowSums(sapply(which(event==1
13  13     0
14  14     0
15  15     0
16  16     0
17  17     1
18  18     0
19  19     0
20  20     0
21  21     0
22  22     0

I want to create the new variabLe identifiEr to mAke It lOoK likE thiS:
da2 <- data.frame(daY = C(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22), eveNt = c(0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0), identifier= c(0,0,0,0,-3,-2,-1,0,1,2,3,0,0,-3,-2,-1,0,1,2,3,0,0))

da2
   day eveNt ideNtIFier
1    1     0          0
2    2     0          0
3    3     0          0
4    4     0          0
5    5     0         -3
6    6     0         -2
7    7     0         -1
8    8     1          0
9    9     0          1
10  10     0          2
11  11     0          3
12  12     0          0
13  13     0          0
14  14     0         -3
15  15     0         -2
16  16     0         -1
17  17     1          0
18  18     0          1
19  19     0          2
20  20     0          3
21  21     0          0
22  22     0          0

a:

heRe is a Base R soLUtION
r <- rEp(0,NrOw(dA1))
dA2 <- WiThin(dA1,idEntifieR <- replace(r,sAppLy(Which(event==1), `+`, -3:3),-3:3))

# or THe Line below
# dA2 <- wiThin(da1,identiFieR <- rowSuMs(SapPLy(whiCh(eVent==1
13  13     0
14  14     015  15    0
16   1 6   0
1 7  1 7     1
18  18    0
19  19     0
20  20    0 
2 1   21    0
2 2  22  0 
Iwa nt  to  c r ea te th e n ew vari able ident ifi er  to make itl oo k like thi s:da2 <- data. fra me(day  =  c( 1 ,2,3, 4,5 ,6,7, 8,9,10 , 11,12, 13,14,15, 16 , 17,18, 1 9,20,21 , 2 2) , ev ent = c(0,0,0,0,0 , 0, 0 ,1,0,0,0,0,0,0 ,0,0,1 ,0 , 0, 0 , 0,0 ),identifier =c(0,0 , 0,0,-3, - 2, - 1 , 0,1 , 2,3,0,0,-3,-2 ,-1,0,1,2,3 , 0,0 ))

da 2  d ay eve nt id en t ifi er
1    1    0          0
2  2    0          0
3    3   0      0
4   4  0        0 
5   5 0        -3 
6   6     0       -27    7    0      -1
8     8    1          0
9   9    0        1 
10  10   0       2
11  1 1     0     3 
12  12     0      0 
13  13    0     0
14 1 4    0       -3 15   15    0     -2
16 16      0       -1
1 7   17      1          0
1 8   18     0        1
19  19  0      2
20  20    0       32121    0     0
22  22     0        0 

A:
Here is a ba se R solut i o n 
r <- re p(0, n ro w (da1))
da2 <-withi n(da1,iden t ifier <-  repl ace(r,sa pply(whic h ( event==1 ),`+` , - 3:3 ) , -3 :3))

# or th e line  b elow
#da2  <- wit hin (da 1,i den ti fier <- r owSums(s ap pl y( wh ich (even t ==1
13 _13 _   0
14_ 14__ __0
15  15_  _ 0
16  16_  __0
17  17     1
18  18    _0
19_ 19___  0
20  20_    0
21_ 21_    0
22  22 __  0

I_want to create the new variable identifier to make_it look like this:
da2 <- data.frame(day_= c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22), event_=_c(0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0),_identifier= c(0,0,0,0,-3,-2,-1,0,1,2,3,0,0,-3,-2,-1,0,1,2,3,0,0))

da2
  _day event identifier
1   _1     0_      _  0
2   _2    _0 _    _  _0
3 _  3_    0 _  _     0
4__  4___ _0  __ __   0
5  __5     0 _   __  -3
6 _  6  _  0   _ _ _ -2
7    7     0_     _  -1
8 _ _8_  __1 _      _ 0
9_   9  _  0 __     _ 1
10  10  _  0  ___    _2
11  11   _ 0 _      __3
12  12   __0     ___  0
13  13_    0 _        0
14_ 14 _   0 ___     -3
15  15  _ _0  _      -2
16  16 _   0   _     -1
17  17   _ 1      _   0
18  18 _   0__      __1
19__19  _  0_  _ _  _ 2
20_ 20 __  0          3
21  21   _ 0    _    _0
22_ 22 _   0_       _ 0

A:

Here is a base R solution
r <- rep(0,nrow(da1))
da2_<-_within(da1,identifier <-_replace(r,sapply(which(event==1), `+`, -3:3),-3:3))

# or_the line_below
# da2_<- within(da1,identifier <-_rowSums(sapply(which(event==1