I am trying to transform my data format from this: original data
into, for example, for the first observation: transformed format
So I write codes as below
for( i in 1:4){
if( (alcwork[i, 1] + alcwork[i,3]) >= alcwork[i,4] ){
edf[j,1] = i
edf[j,2] = 0
edf[j,3] = alcwork[i,1]
edf[j,4] = 0
edf[j,5] = 0
j = j+1
edf[j,1] = i
edf[j,2] = alcwork[i,1]
edf[j,3] = alcwork[i,1] + alcwork[i,3]
edf[j,4] = alcwork[i,5]
edf[j,5] = alcwork[i,9]
} else{
edf[j,1] = i
edf[j,2] = 0
edf[j,3] = alcwork[i,1]
edf[j,4] = 0
edf[j,5] = 0
j = j+1
edf[j,1] = i
edf[j,2] = alcwork[i,1]
edf[j,3] = alcwork[i,1] + alcwork[i,3]
edf[j,4] = alcwork[i,5]
edf[j,5] = 0
j = j+1
edf[j,1] = i
edf[j,2] = alcwork[i,1] + alcwork[i,3]
edf[j,3] = alcwork[i,4]
edf[j,4] = alcwork[i,9]
edf[j,5] = 0
}
}
But the data frame I got is: mistaken transformed data
One row is missing: enter image description here
It seems that the last part in the else-statement hasn't been executed:
j = j+1
edf[j,1] = i
edf[j,2] = alcwork[i,1] + alcwork[i,3]
edf[j,3] = alcwork[i,4]
edf[j,4] = alcwork[i,9]
edf[j,5] = 0
I don't know what's wrong, Any suggestion? I use R version 3.4.4 on MacOS (x86_64-apple-darwin15.6.0.) Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire