Jay khan
Jay khan

Reputation: 745

Fill in the values based on first cell value

Suppose I have this data

ProductID   VarA    VarB    VarC    VarD    Month   UDP
 1000        A       K       X       0       1       10
 1000        B       L       X       0       2       10.5
 1000        C       M       X       0       3       11
 1000        C       K       X       0       4       12
 1000        C       L       X       0       5       12.4
 1000        B       M       X       0       6       12.5
 1001        A       K       Y       0       1       0
 1001        B       L       Y       0       2       0
 1001        C       M       Y       0       3       0
 1001        A       K       Y       0       4       0
 1001        A       L       Y       0       5       0
 1001        B       M       Y       0       6       0
 1002        C       K       Z       1       1       0
 1002        A       L       Z       1       2       0
 1002        A       M       Z       1       3       0
 1002        B       K       Z       1       4       0
 1002        C       L       Z       1       5       0
 1002        A       M       Z       1       6       0

I have UDP values for one product for 6 months. I want to copy the values to all the other products. So the data looks like this

ProductID   VarA    VarB    VarC    VarD    Month   UDP
 1000        A       K       X       0       1       10
 1000        B       L       X       0       2       10.5
 1000        C       M       X       0       3       11
 1000        C       K       X       0       4       12
 1000        C       L       X       0       5       12.4
 1000        B       M       X       0       6       12.5
 1001        A       K       Y       0       1       10
 1001        B       L       Y       0       2       10.5
 1001        C       M       Y       0       3       11
 1001        A       K       Y       0       4       12
 1001        A       L       Y       0       5       12.4
 1001        B       M       Y       0       6       12.5
 1002        C       K       Z       1       1       10
 1002        A       L       Z       1       2       10.5
 1002        A       M       Z       1       3       11
 1002        B       K       Z       1       4       12
 1002        C       L       Z       1       5       12.4
 1002        A       M       Z       1       6       12.5

Upvotes: 1

Views: 35

Answers (1)

akrun
akrun

Reputation: 887541

We can use rep

 df1$UDP <- rep(df1$UDP[df1$UDP!=0], length(unique(df1$ProductID)))

Upvotes: 2

Related Questions