R: filling and / or duplicating rows based on other columns

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 
+5
source share
1 answer

With tidyr and dplyr you can do something next to what you are aiming for.

 library(tidyr) library(dplyr) # replacing empty string with NA df <- df %>% mutate_each(funs(sub("^$", NA, .)), Part, type_p) # filling missing values df <- df %>% fill(Part, type_p,.direction = "down") %>% fill(Part, type_p,.direction = "up") 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 tom pan 2 #> 7 B tom pan 3 

To get what you described (in the question and comments), you could separately process specifications A and B:

 bind_rows( df %>% filter(bom == "A"), df %>% filter(bom == "B") %>% complete(nesting(bom, Part, type_p), ww) ) #> Source: local data frame [10 x 4] #> #> bom Part type_p ww #> (fctr) (chr) (chr) (dbl) #> 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 #> 8 B tom pan 1 #> 9 B tom pan 2 #> 10 B tom pan 3 
+3
source

Source: https://habr.com/ru/post/1244158/


All Articles