# Hypothesis test comparing proportions while controlling for the effect of a nuisance variable

I have data that looks like this:

personID <- 1:20
country <- c(rep(1,10), rep(2, 10))
success <- c(rbinom(10, 1, .2), rbinom(10, 1, .8))
income <- c(100, 100, 300, 200, 200, 200, 150, 300, 100, 100, 400, 300, 300, 250, 300, 350, 500, 200, 200, 300)

x <- data.frame(personID = personID, country = country, success = success, income = income)

> x
personID country success income
1         1       1       0    100
2         2       1       0    100
3         3       1       0    300
4         4       1       1    200
5         5       1       0    200
6         6       1       1    200
7         7       1       1    150
8         8       1       0    300
9         9       1       0    100
10       10       1       0    100
11       11       2       1    400
12       12       2       1    300
13       13       2       1    300
14       14       2       1    250
15       15       2       1    300
16       16       2       1    350
17       17       2       1    500
18       18       2       0    200
19       19       2       1    200
20       20       2       1    300


I want to test the rate of success (success = 1) of the two countries (with a hypothesis test where the null hypothesis is that there is no difference in the rate of success between the two countries), but while controlling for the effect of income. Without wanting to control for income, this would be easy, as I could just set up a two-sample Z-test comparing the two proportions of success for each country. But how can I test the proportions while controlling for the new variable, income?