In the following data, I would like to add another variable say z
.
mydata
y x sl
1 199.92989 1 1
2 27.73883 2 1
3 144.00000 3 1
4 72.00000 4 1
5 0.00000 5 1
6 392.60636 1 2
7 749.52499 2 2
8 3120.00000 3 2
9 1600.00000 4 2
10 1000.00000 5 2
11 5840.00000 6 2
12 3960.00000 7 2
13 4700.00000 8 2
14 1660.00000 9 2
15 5620.00000 10 2
16 0.00000 1 585
17 0.00000 2 585
18 0.00000 3 585
19 3062.32962 1 587
20 2048.97458 2 587
21 1280.00000 3 587
22 1440.00000 4 587
23 2960.00000 5 587
24 460.00000 6 587
25 530.00000 7 587
26 5190.00000 8 587
27 3200.00000 9 587
28 4620.00000 10 587
29 0.00000 1 651
30 0.00000 2 651
31 0.00000 3 651
32 0.00000 4 651
z=c(5,7,8)
, The value 5
should be repeated 5 times and belongs to sl=1
, 7
should be repeated 10
times and belongs to sl=2
, 8
should be repeated 10
times and belongs to sl=587
, . If all the observations of y
are for 0
for any sl
say 585
and 651
, then z
must take value 0
. the z
column must be like this z=c(rep(5,5), rep(7,10), rep(0,3), rep(8,10), rep(0,4))=c(5 5 5 5 5 7 7 7 7 7 7 7 7 7 7 0 0 0 8 8 8 8 8 8 8 8 8 8 0 0 0 0)
How can I do it with the above conditions?