Reducing variables into one variable according to a heuristic

I'm having problems in searching for my issue, because I haven't been able to phrase it :worried:, so I'll try to explain it with an example:

I got at least 4 columns (addr1...addr4 ; from different sources). I need to reduce those 4 columns to 1. For some rows, I get the four of them, for others only one, some I get addr1 and addr3, ecc.

The reduction is somewhat simple: theres one (addr2) that's more reliable, so I always should keep that one, if not, I should continue in a specific order (then addr3, then addr1...). It's a very simple algorithm.

I have managed to do it in a non very elegant way using a mix of mutate and ifelse branching, but I was wondering if there's a better way.

As I said, I didn't know how to "phrase" this issue, so searching was very unhelpful :sob:

HI @pablo,
You will need to post your code plus a small sample of the input data, and the desired output. This shows us what is working and may elicit responses with possible improvements.


#example data

(df1 <- tibble(
  col4=c(NA ,"w","z")

(df2<- mutate(df1,
             best = case_when(!,
                              TRUE ~ NA_character_

It's more like a general, maybe even could be tagged as non-coding question. I believe the answer was using case_when, but I need to check it.

Thanks a lot, it seems it could solve the problem (I'll try it asap), Thanks a lot :).

Anyway, I'm still wondering how this issue could be categorised, how should I have search it?

Thanks a lot, I marked it as solved. Can you help me understand why this works?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.