bugs in conditional ifelse

I think we really need a Reproducible Example. What you provided before was just an image. To make a reproducible example, you need to post here the code to make example data and the code used to process that example data set. Making the example data can be done using datapasta or dput as explained under the Minimal Dataset heading of the post linked below. If you cannot do that, just use the data.frame() function and type out your data. The beginning of that process might look like this

y <- data.frame(x1 = c("d", "d1", "d2", "d3", "d4", "d", "d", "d", "dd", "d", "d", "d1"),
                x2 = c("d", ...More Typing))

Type out enough rows of data to illustrate your problem, then post that code, the processing code and the observed result. The reprex package will help you make an easily copied post on the forum.