vendredi 22 décembre 2017

Create a new variable based on existing variable

My current dataset look like this

Order     V1     
1         7     
2         5    
3         8     
4         5  
5         8 
6         3    
7         4  
8         2     
1         8    
2         6 
3         3 
4         4 
5         5 
6         7 
7         3 
8         6 

I want to create a new variable called "V2" based on the variables "Order" and "V1". For every 8 items in the "Order" variable, I want to assign a value of "0" in "V2" if the varialbe "Order" has observation equals to 1; otherwise, "V2" takes the value of previous item in "V1". This is the dataset that I want

Order    V1    V2
1        7     0
2        5     7
3        8     5
4        5     8
5        8     5
6        3     8
7        4     3
8        2     4
1        8     0
2        6     8
3        3     6
4        4     3
5        5     4
6        7     5
7        3     7
8        6     3

Since my actual dataset is very large, I'm trying to use for loop with if statement to generate "V2". But my code keeps failing. I appreciate if anyone can help me on this, and I'm open to other statements. Thank you!

Aucun commentaire:

Enregistrer un commentaire