I have a large dataframe where names (col1) should be nested within location name (col2). In the example below "d" from col1 should be within "z" of col2 but is listed as "y" in the eighth element. I cannot just rename "y" in col2 as it is correct in most places. I need to rename "y" to "z" only when col1 == "d". It is a large dataframe with multiple example so it is also not possible just to rename the element
col1<-c("a","b","c","d","a","b","c","d")
col2<-c("x","y","z","z","x","y","z","y")
df<-data.frame(col1,col2)