Recoding "all other values" of a Variable into one value

Hi everyone,
first day in here and already a question. I have a problem quite similar to this:
(SOLVED: Recode variable from text to numbers)
I found that in here on my quest for a solution.
But unlike the original User, my data contains also free text strings, which i want to recode with a certain number. So my Data looks like

Data$Variable <- c(Germany, Switzerland, Bolivia, Togo,...)

and i want to recode it like

"Germany"=1, "Switzerland"=2 ...

but, here's the thing: Bolivia, Togo and some other more exotic Countries should get assigned the Number "3".
I have solved the recoding/mutating part, but i can't find a appropriate expression for "all the rest"=3.
I have tried several if-then-else or dplyr::ifelse constructions, but those didn't work out. Mainly because i'm confused by logical arguments, vectors, data frames and all that stuff.
So, my question is: is there an expression for "all other values" or how can i solve it in another fashion?
I'm quite new to R and RStudio (as you can tell from my question) and i hope helping me isn't too boring...
Best regards

Hi, thanks for trying to provide some properly formatted sample data but you have missed some details to make it actually usable by us, for future reference on how to provide a proper reproducible example, please read the guide on this link

About your problem at hand, you can use the case_when() function from dplyr, see this example (Please notice the way I'm sharing it with you, that would be a proper reprex).

library(dplyr)

# Sample data on a copy/paste friendly format
Data <- data.frame(
    Variable = c("Germany", "Switzerland", "Bolivia", "Togo")
)

# Relevant code
Data %>% 
    mutate(
        new_variable = case_when(
            Variable == "Germany" ~ 1,
            Variable == "Switzerland" ~ 2,
            TRUE ~ 3
        )
    )
#>      Variable new_variable
#> 1     Germany            1
#> 2 Switzerland            2
#> 3     Bolivia            3
#> 4        Togo            3

Created on 2022-04-30 by the reprex package (v2.0.1)

Great, i appreciate your quick help.
I will try this approach on Monday, but it looks feasible to me. I promise to give better reprex next time :raised_hand:

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.