My question is based on this question .
I have data as shown below. I want to fill in the cells by first looking down and then looking up until the same one. In the case of bom = A, I want to fill in the lines as shown. But in the case of bom = B, since the type_p column is different, I want to duplicate rows and feel the spaces
bom=c(rep("A",4),rep("B",3)) Part=c("","lambda","beta","","tim","tom","") type_p=c("","sub","sub","","sub","pan","") ww=c(1,2,3,4,1,2,3) df=data.frame(bom,Part,type_p,ww) > df bom Part type_p ww 1 A 1 2 A lambda sub 2 3 A beta sub 3 4 A 4 5 B tim sub 1 6 B tom pan 2 7 B 3
The final data I want is below
bom Part type_p ww 1 A lambda sub 1 2 A lambda sub 2 3 A beta sub 3 4 A beta sub 4 5 B tim sub 1 6 B tim sub 2 7 B tim sub 3 5 B tom pan 1 6 B tom pan 2 7 B tom pan 3
________________________________________ Update 1
The logic I need is as shown below. Remember that my data is very large and I have thousands of values ββin each column.
the bom and ww columns are always populated / populated by incoming data
- Check if the entry in the bom column has more than 1 value in the type_p column
- If there is only 1 value, fill in the blanks in the type_p and ww columns, first looking down and then looking up. In this case, bom = A has only one value in type_p (sub)
- If the entry in the bom column has more than one unique value in the type_p column, then create additional sets of identical rows in this group, so that the common sets will be equal to the different values ββin the type_p column for this bom. In this case, bom = B has two values ββin type_p (sub and pan)
- Fill in the blanks in the type_p and ww columns, first looking down and then looking up (look at the original row to fill in the values)
============================================= ====== ========= Update 2
After step 3, the data frame will look below
> df bom Part type_p ww 1 A lambda sub 1 2 A lambda sub 2 3 A beta sub 3 4 A beta sub 4 5 B tim sub 1 6 B 2 7 B 3 8 B 1 9 B tom pan 2 10 B 3