我正在重建R中的VBA代码,根据不同的条件计算从评级到另一个的转换:
如下:
## attach the relevant data table
attach(cohort)
# define the matrices that will contain all the counting information
ni = matrix(0,nrow = 1, ncol = classes - 1)
nij = matrix(0, nrow = classes-1, ncol = classes+1)
for (k in 1:obs)
{
# define the year of the kth observation
t = apply(data.frame(date[k],ystart),1,max, na.rm = F)
#t = year(as.Date(t))
while (t < yend)
{
# if this observation and the second one belong to the same id and year, break and move to the next one
if (id[k] == id[k+1] & date[k] == date[k+1]) {break}
# if the rating of this observation is 0 (not rated) or in default, then leave it
if (rating[k] == classes | rating[k] == 0) {break}
# add to the group of customers with rating = rating_k, 1 observation
rating_k = rating[k]
ni[rating_k] = ni[rating_k]+1
# determine the rating from end of next year
if (id[k] != id[k+1] | date[k+1] > (t+1))
{newrat = rating_k}
else
{
kn = k +1
while (date[kn]==date[kn+1] & id[kn]==id[kn+1])
{
if (rating[kn]==classes) {break}
Kn = kn+1
}
newrat = rating[kn]
}
nij[rating_k, newrat] = (nij[rating_k, newrat] + 1)
if(newrat!=rating[k]) {break}
else
{t = (t+1)}
}
print (k)
}
在我的代码结束时,如果条件&#34; if(newrat!= rating [k])&#34;满足,我希望我的代码打破并移动到下一个K.否则,如果条件不满足,我有t = t + 1,其中代码将返回到while内的条件(t
我最后添加了&#34; print(k)&#34;了解哪个&#34;对于k ...&#34;步骤代码停止,并且它总是停在k = 9,同时打印k = 1到8。总的来说,我有4000个观察值,但只考虑了8个,尽管循环从不停止并且R继续运行。