I am struggling to make loop through the rows of a column in a dataframe and then use the current row to define arguments that will be used in a function. Here is the sample dataframe:
df <-
structure(list(child = c("A268", "A268497", "A268497BOX", "A268497BOX2",
"A268497BOX218", "A277", "A277A79", "A277A79091", "A277A790911",
"A277A79091144", "A492", "A492586", "A492586BOX", "A492586BOX1",
"A492586BOX144", "A492A69", "A492A69027", "A492A690271", "A492A69027144",
"A492A6902715K", "A492A6902719Y", "A492A690271BH", "A492A690271BI",
"A492A690271CQ", "A492A690271CS", "A492A690271CT", "A492A690271CU",
"A492A690271CV", "A492A690271CW", "A492A690271CX", "A492A690271CY",
"A492A690271DA", "A492A69028", "A492A690281", "A492A69028144",
"A492A69402", "A492A694021", "A492A69402144", "A492A70", "A492A70033",
"A492A700331", "A492A70033144", "A492A700332", "A492A70033244",
"A492A70034", "A492A700341", "A492A70034144", "A492A70035", "A492A700351",
"A492A70035144"), clvl = c(2, 3, 4, 5, 6, 2, 3, 4, 5, 6, 2, 3,
4, 5, 6, 3, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4,
5, 6, 4, 5, 6, 3, 4, 5, 6, 5, 6, 4, 5, 6, 4, 5, 6), parent = c("A",
"A268", "A268497", "A268497BOX", "A268497BOX2", "A", "A277",
"A277A79", "A277A79091", "A277A790911", "A", "A492", "A492586",
"A492586BOX", "A492586BOX1", "A492", "A492A69", "A492A69027",
"A492A690271", "A492A690271", "A492A690271", "A492A690271", "A492A690271",
"A492A690271", "A492A690271", "A492A690271", "A492A690271", "A492A690271",
"A492A690271", "A492A690271", "A492A690271", "A492A690271", "A492A69",
"A492A69028", "A492A690281", "A492A69", "A492A69402", "A492A694021",
"A492", "A492A70", "A492A70033", "A492A700331", "A492A70033",
"A492A700332", "A492A70", "A492A70034", "A492A700341", "A492A70",
"A492A70035", "A492A700351"), plvl = c(1, 2, 3, 4, 5, 1, 2, 3,
4, 5, 1, 2, 3, 4, 5, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 3, 4, 5, 3, 4, 5, 2, 3, 4, 5, 4, 5, 3, 4, 5, 3, 4, 5
)), row.names = c(NA, 50L), class = "data.frame")
My goal is to generate this:
I tried to do this with a loop and using different versions of apply function inside the loop, but I could not get it right. Here, I need to define that x and y will be the child and pathString from the current row every time I iterate. Is there a clean and easy way to do this?
df[] <- apply(df,1,function(x,y) sub(x,y,x))


pathStringvariable ?