0

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!

2 Answers 2

1

(Up front: I am assuming that the order of Order is perfectly controlled.)

You need simply ifelse and lag:

df <- read.table(text="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 ", header=T)

df$V2 <- ifelse(df$Order==1, 0, lag(df$V1))
df
#    Order V1 V2
# 1      1  7  0
# 2      2  5  7
# 3      3  8  5
# 4      4  5  8
# 5      5  8  5
# 6      6  3  8
# 7      7  4  3
# 8      8  2  4
# 9      1  8  0
# 10     2  6  8
# 11     3  3  6
# 12     4  4  3
# 13     5  5  4
# 14     6  7  5
# 15     7  3  7
# 16     8  6  3
Sign up to request clarification or add additional context in comments.

1 Comment

Or simple: ifelse(df$Order == 1, 0, c(NA, df$V1))
1
with(dat,{V2<-c(0,head(V1,-1));V2[Order==1]<-0;dat$V2<-V2;dat})
   Order V1 V2
1      1  7  0
2      2  5  7
3      3  8  5
4      4  5  8
5      5  8  5
6      6  3  8
7      7  4  3
8      8  2  4
9      1  8  0
10     2  6  8
11     3  3  6
12     4  4  3
13     5  5  4
14     6  7  5
15     7  3  7
16     8  6  3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.