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.