Merge two variables

Hello
I am pretty new to R, and I am struggling with my dataset.
I have multiple variables from the ESS round 8 dataset containing information about the last party individuals voted for in the last national election. I have made them into dummy variables, depending on whether they voted for a green party or not. The problem is that they are divided by country, and I need them all to be in one single column in my dataset. Does anyone know how I could conduct such a command?

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

So this is what I´ve tried to do so far (excuse the Norwegian names)

e$Stemtegrønt <- ifelse(e$Island == "1", 1, 
  ifelse(e$Belgia == "1", 1,
    ifelse(e$ValgØsterriket == "1", 1,
      ifelse(e$ValgTyskland1 == "1", 1,
        ifelse(e$ValgEstland == "1", 1,
          ifelse(e$ValgFinland == "1", 1,
            ifelse(e$ValgFrankrike == "1", 1,
              ifelse(e$ValgStorbritannia == "1", 1,
                ifelse(e$Irland == "1", 1,
                  ifelse(e$Island == "1", 1,
                    ifelse(e$ValgLitauen1 == "1", 1,
                      ifelse(e$ValgNederland == "1", 1,
                        ifelse(e$ValgNorge == "1", 1,
                          ifelse(e$ValRussland == "1", 1,
                            ifelse(e$ValgSverige == "1", 1,
                              ifelse(e$ValgSveits == "1",  1,
                                ifelse(e$ValgPortugal == "1", 1, 0, NA)))))))))))))))))

So I tried to make one single variable with all these individual ones, but I got an error code of unexpected symbol when I inserted the 0 and NA. I basically want to include both the yes and no answers (zero and one), and I also have to include all the NAs.

Since you're new here, it might be helpful to know how to properly format code and console output that you post here. Using proper code formatting makes the site easier to read, prevents confusion (unformatted code can get garbled by the forum software :anguished:), and is generally considered the polite thing to do. Check out this FAQ to find out how — it's as easy as the click of a button! :grinning::

I've formatted your code and added some missing parentheses.

ifelse is rarely the best way to do something. Nested ifelse calls are better served by using plain logical operators. Personally, I find they also make the code's purpose clear to anyone reading it.

e <- data.frame(
  Island = c("0", "1", NA),
  Belgia = c("0", "0", "0"),
  ValgØsterriket = c("0", "0", "0"),
  ValgTyskland1 = c("0", "0", "0"),
  ValgEstland = c("0", "0", "0"),
  ValgFinland = c("0", "0", "0"),
  ValgFrankrike = c("0", "0", "0"),
  ValgStorbritannia = c("0", "0", "0"),
  Irland = c("0", "0", "0"),
  ValgLitauen1 = c("0", "0", "0"),
  ValgNederland = c("0", "0", "0"),
  ValgNorge = c("0", "0", "0"),
  ValRussland = c("0", "0", "0"),
  ValgSverige = c("0", "0", "0"),
  ValgSveits = c("0", "0", "0"),
  ValgPortugal = c("0", "0", "0"),
  stringsAsFactors = FALSE
)


check_cols <- c(
  "Island", "Belgia", "ValgØsterriket", "ValgTyskland1",
  "ValgEstland", "ValgFinland", "ValgFrankrike",
  "ValgStorbritannia", "Irland", "ValgLitauen1",
  "ValgNederland", "ValgNorge", "ValRussland", "ValgSverige",
  "ValgSveits", "ValgPortugal"
)

is_one <- e[, check_cols] == "1"
row_has_one <- apply(is_one, MARGIN = 1, FUN = any)
row_has_na <- apply(e[, check_cols], MARGIN = 1, FUN = anyNA)
e$Stemtegrønt <- "0"
e$Stemtegrønt[row_has_one] <- "1"
e$Stemtegrønt[row_has_na] <- NA

e$Stemtegrønt
# [1] "0" "1" NA

Sticking to your code, I can't reproduce the exact error you mention (unexpected symbol), but I do get a different one:

ifelse(e$Island == "1", 1, 
  ifelse(e$Belgia == "1", 1,
    ifelse(e$ValgØsterriket == "1", 1,
      ifelse(e$ValgTyskland1 == "1", 1,
        ifelse(e$ValgEstland == "1", 1,
          ifelse(e$ValgFinland == "1", 1,
            ifelse(e$ValgFrankrike == "1", 1,
              ifelse(e$ValgStorbritannia == "1", 1,
                ifelse(e$Irland == "1", 1,
                  ifelse(e$Island == "1", 1,
                    ifelse(e$ValgLitauen1 == "1", 1,
                      ifelse(e$ValgNederland == "1", 1,
                        ifelse(e$ValgNorge == "1", 1,
                          ifelse(e$ValRussland == "1", 1,
                            ifelse(e$ValgSverige == "1", 1,
                              ifelse(e$ValgSveits == "1",  1,
                                ifelse(e$ValgPortugal == "1", 1, 0, NA)))))))))))))))))
# Error in ifelse(e$ValgPortugal == "1", 1, 0, NA) :
# unused argument (NA)

ifelse only takes three arguments: test, yes, and no. But your last ifelse has 4 arguments.

1 Like

Oh, sorry! I am so new to R that everything is just so confusing at the moment. Thank you so much for your help btw, I´ve been struggling with this all day and I finally did it :slight_smile:

1 Like

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