t*********e 发帖数: 143 | 1 现有一个table. sample结构如下
a b c d
10 1 2 15
10 2 2 14
10 3 2 24
10 4 2 35
12 1 3 11
12 2 3 148
12 3 3 27
12 4 3 19
12 5 3 8
25 1 7 4
25 2 7 8
table 以a,b ascending为index.
现在想要达成如下结果
对每一个a,b组合值,根据c和d的值生成一个step方程的值.
e=0 if b<=c
e=2+18*(lag of d by the value of c-5)^2
比如。
for a=10, b=1, since b=1
a=10, b=2, since b=c=2, then e=0
a=10, b=3, since b=3 > c=2, then e=2+18*(15-5)^2=1802, 15是d lag 2生
成的
a=10, b=4, since b=4 > c=2, then e=2+18*(14-5)^2=
a=12, b=1, since b=1
a=12, b=2, since b=2
请问有没有什么简便的方法达到想要的结果,对我来说最大的问题是lag的处理,它是depend on c的
如果解决大包子送上 | D******n 发帖数: 2836 | 2 use R
【在 t*********e 的大作中提到】 : 现有一个table. sample结构如下 : a b c d : 10 1 2 15 : 10 2 2 14 : 10 3 2 24 : 10 4 2 35 : 12 1 3 11 : 12 2 3 148 : 12 3 3 27 : 12 4 3 19
| s******y 发帖数: 352 | 3 data have;
input a b c d;
cards;
10 1 2 15
10 2 2 14
10 3 2 24
10 4 2 35
12 1 3 11
12 2 3 148
12 3 3 27
12 4 3 19
12 5 3 8
25 1 7 4
25 2 7 8
;
run;
data want;
call missing(of lagd);
set have;
howmany=c;
if b<=c then e=0;
else if b>c and _n_-howmany>0 then do back=_n_-1 by -1 while(howmany>0) ;
set have(rename=(d=lagd)keep=d) point=back ;
howmany=sum(howmany,-1);
exp=cats('2+18*(',lagd,'-5)^2');
e=2+18*(lagd-5)**2;
end;
drop lagd howmany;
run;
【在 t*********e 的大作中提到】 : 现有一个table. sample结构如下 : a b c d : 10 1 2 15 : 10 2 2 14 : 10 3 2 24 : 10 4 2 35 : 12 1 3 11 : 12 2 3 148 : 12 3 3 27 : 12 4 3 19
| D******n 发帖数: 2836 | 4 can be more concise
data a2;
set a1;
if b<=c then e=0;
else if _n_-c>0 then do;
back = _n_ - c;
set a1(rename=(d=lag_d) keep=d) point=back;
e = 2+18*(lag_d-5)**2;
end;
drop lag_d;
run;
proc print;run;
【在 s******y 的大作中提到】 : data have; : input a b c d; : cards; : 10 1 2 15 : 10 2 2 14 : 10 3 2 24 : 10 4 2 35 : 12 1 3 11 : 12 2 3 148 : 12 3 3 27
| s******y 发帖数: 352 | 5 Very good point. the loop is redundant. we just need to rewind back C times,
no need to keep track the current position.
I wouldn't leave the call missing off. because the lag_d is sort of self
retained. most of time you don't want that happens.
【在 D******n 的大作中提到】 : can be more concise : data a2; : set a1; : if b<=c then e=0; : else if _n_-c>0 then do; : back = _n_ - c; : set a1(rename=(d=lag_d) keep=d) point=back; : e = 2+18*(lag_d-5)**2; : end; : drop lag_d;
| S******y 发帖数: 1123 | 6 #Python 2.6
in_file = r'C:\abcd.txt'
f = open(in_file, 'r')
f.next() #skip header row
ls_d = []
for line in f:
a,b,c,d = line.split()
if int(b) <= int(c): e = 0
else: e = 2 + 18 * (ls_d[-int(c)]-5) * (ls_d[-int(c)]-5)
print a, b, c, d, e
ls_d.append(int(d))
#---------------------------------------------------# |
|