I have a dataframe with 3 columns, but in the third column there are some missing values that need to be filled with the following logic.
The desired result is the absence of missing values in the third column (part), to fill it, we must find the first "week" in which there is a "part" filled for this specific "element" (1st column), and then fill it by copying the “part” until a new “part” value is found. Then repeat the copying process until a new line "item" is found.
Finally, repeat the filling process for each “element” that is now growing, as there may have been some gaps before the first “week”, the “part” was filled there.
Here is an example data frame:
item=c(rep("A",7),rep("B",3),rep("D",5)) part=c("","","X","","Y","","","","Z","","","T","U","","") week=c("1","2","3","4","5","6","7","1","2","3","10","11","12","13","14") df=data.frame(item,week,part)
The desired resulting data frame is as follows:
item2=c(rep("A",7),rep("B",3),rep("D",5)) part2=c("X","X","X","X","Y","Y","Y","Z","Z","Z","T","T","U","U","U") week2=c("1","2","3","4","5","6","7","1","2","3","10","11","12","13","14") df2=data.frame(item2,week2,part2)
Help evaluate, thanks.