-1

I always get the following error in my code:

unexpected ) in ifelse(Kt_15==1&agegroup_15=="91+"&hosp_14==0&male==1,59, ifelse(Kt_15==1&agegroup_15=="91+"&hosp_14==1&male==1,60,0)))))))))))

I have about 1,000,000 observations in my dataset called RA. With the conditions below I want to mutate a new variable called RG1. If I only use the first 10 lines and let them run, the code works perfectly fine; but 60 seems to be too much for R. I already counted the amount of brackets a dozens of times and a friend of mine double-checked them.

RA = RA %>%
mutate(RG1=
ifelse(Kt_15==1&agegroup_15=="19-25"&hosp_14==0&male==0,1,
ifelse(Kt_15==1&agegroup_15=="19-25"&hosp_14==1&male==0,2,
ifelse(Kt_15==1&agegroup_15=="19-25"&hosp_14==0&male==1,3,
ifelse(Kt_15==1&agegroup_15=="19-25"&hosp_14==1&male==1,4,
ifelse(Kt_15==1&agegroup_15=="26-30"&hosp_14==0&male==0,5,
ifelse(Kt_15==1&agegroup_15=="26-30"&hosp_14==1&male==0,6,
ifelse(Kt_15==1&agegroup_15=="26-30"&hosp_14==0&male==1,7,
ifelse(Kt_15==1&agegroup_15=="26-30"&hosp_14==1&male==1,8,
ifelse(Kt_15==1&agegroup_15=="31-35"&hosp_14==0&male==0,9,
ifelse(Kt_15==1&agegroup_15=="31-35"&hosp_14==1&male==0,10,
ifelse(Kt_15==1&agegroup_15=="31-35"&hosp_14==0&male==1,11,
ifelse(Kt_15==1&agegroup_15=="31-35"&hosp_14==1&male==1,12,
ifelse(Kt_15==1&agegroup_15=="36-40"&hosp_14==0&male==0,13,
ifelse(Kt_15==1&agegroup_15=="36-40"&hosp_14==1&male==0,14,
ifelse(Kt_15==1&agegroup_15=="36-40"&hosp_14==0&male==1,15,
ifelse(Kt_15==1&agegroup_15=="36-40"&hosp_14==1&male==1,16,
ifelse(Kt_15==1&agegroup_15=="41-45"&hosp_14==0&male==0,17,
ifelse(Kt_15==1&agegroup_15=="41-45"&hosp_14==1&male==0,18,
ifelse(Kt_15==1&agegroup_15=="41-45"&hosp_14==0&male==1,19,
ifelse(Kt_15==1&agegroup_15=="41-45"&hosp_14==1&male==1,20,
ifelse(Kt_15==1&agegroup_15=="46-50"&hosp_14==0&male==0,21,
ifelse(Kt_15==1&agegroup_15=="46-50"&hosp_14==1&male==0,22,
ifelse(Kt_15==1&agegroup_15=="46-50"&hosp_14==0&male==1,23,
ifelse(Kt_15==1&agegroup_15=="46-50"&hosp_14==1&male==1,24,
ifelse(Kt_15==1&agegroup_15=="51-55"&hosp_14==0&male==0,25,
ifelse(Kt_15==1&agegroup_15=="51-55"&hosp_14==1&male==0,26,
ifelse(Kt_15==1&agegroup_15=="51-55"&hosp_14==0&male==1,27,
ifelse(Kt_15==1&agegroup_15=="51-55"&hosp_14==1&male==1,28,
ifelse(Kt_15==1&agegroup_15=="56-60"&hosp_14==0&male==0,29,
ifelse(Kt_15==1&agegroup_15=="56-60"&hosp_14==1&male==0,30,
ifelse(Kt_15==1&agegroup_15=="56-60"&hosp_14==0&male==1,31,
ifelse(Kt_15==1&agegroup_15=="56-60"&hosp_14==1&male==1,32,
ifelse(Kt_15==1&agegroup_15=="61-65"&hosp_14==0&male==0,33,
ifelse(Kt_15==1&agegroup_15=="61-65"&hosp_14==1&male==0,34,
ifelse(Kt_15==1&agegroup_15=="61-65"&hosp_14==0&male==1,35,
ifelse(Kt_15==1&agegroup_15=="61-65"&hosp_14==1&male==1,36,
ifelse(Kt_15==1&agegroup_15=="66-70"&hosp_14==0&male==0,37,
ifelse(Kt_15==1&agegroup_15=="66-70"&hosp_14==1&male==0,38,
ifelse(Kt_15==1&agegroup_15=="66-70"&hosp_14==0&male==1,39,
ifelse(Kt_15==1&agegroup_15=="66-70"&hosp_14==1&male==1,40,
ifelse(Kt_15==1&agegroup_15=="71-75"&hosp_14==0&male==0,41,
ifelse(Kt_15==1&agegroup_15=="71-75"&hosp_14==1&male==0,42,
ifelse(Kt_15==1&agegroup_15=="71-75"&hosp_14==0&male==1,43,
ifelse(Kt_15==1&agegroup_15=="71-75"&hosp_14==1&male==1,44,
ifelse(Kt_15==1&agegroup_15=="76-80"&hosp_14==0&male==0,45,
ifelse(Kt_15==1&agegroup_15=="76-80"&hosp_14==1&male==0,46,
ifelse(Kt_15==1&agegroup_15=="76-80"&hosp_14==0&male==1,47,
ifelse(Kt_15==1&agegroup_15=="76-80"&hosp_14==1&male==1,48,
ifelse(Kt_15==1&agegroup_15=="81-85"&hosp_14==0&male==0,49,
ifelse(Kt_15==1&agegroup_15=="81-85"&hosp_14==1&male==0,50,
ifelse(Kt_15==1&agegroup_15=="81-85"&hosp_14==0&male==1,51,
ifelse(Kt_15==1&agegroup_15=="81-85"&hosp_14==1&male==1,52,
ifelse(Kt_15==1&agegroup_15=="86-90"&hosp_14==0&male==0,53,
ifelse(Kt_15==1&agegroup_15=="86-90"&hosp_14==1&male==0,54,
ifelse(Kt_15==1&agegroup_15=="86-90"&hosp_14==0&male==1,55,
ifelse(Kt_15==1&agegroup_15=="86-90"&hosp_14==1&male==1,56,
ifelse(Kt_15==1&agegroup_15=="91+"&hosp_14==0&male==0,57,
ifelse(Kt_15==1&agegroup_15=="91+"&hosp_14==1&male==0,58,
ifelse(Kt_15==1&agegroup_15=="91+"&hosp_14==0&male==1,59,
ifelse(Kt_15==1&agegroup_15=="91+"&hosp_14==1&male==1,60,0))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))

With the code above, I want to every observation a number from 1 to 60, depending on the mentioned conditions.

  • 8
    I think `ifelse` nesting have a limitation - around 54 or so. A better approach would be to do a `group_by` columns – akrun May 15 '19 at 16:01
  • 2
    Or create a key/val dataset with the60 combinations and do a `left_join` – akrun May 15 '19 at 16:04
  • 2
    This many `ifelse` calls looks like a nightmare to keep track of. Folks can help specifically if you include a [sample of data](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), but I suspect you'll want to switch to a) a key-value lookup like @akrun suggests, b) a `case_when`, c) collapse factor levels, or d) arrange factor levels and just make the observation number by row number. But all I can do is guess without your data. – camille May 15 '19 at 17:26
  • A description of the nesting limit is here: https://stackoverflow.com/a/25063770/2372064. It's more a limit on the number of nested function calls in general than something that's specific to `ifelse`. It really seems like you should be doing a multi-column join instead of an ifelse anyway. – MrFlick May 15 '19 at 17:52

2 Answers2

2

Short answer: don't use so many nested ifelse() functions.
As akrun pointed out in his comment: nesting may have limitations.

I always found nested ifelse() a bit complex to keep track anyway. For me case_when() works better. Documentation is here. The 'starwars' example in the examples of the documentation might be somewhat close to your case.

j_5chneider
  • 390
  • 5
  • 15
1

I am assuming that is the code you and there is real corresponding data. Another option is mentioned by @akrun and @camille.

data %>% 
select(Kt_15, agegroup_15, hosp_14, male) %>%
distinct() %>%
arrange(Kt_15, agegroup_15, male, hosp_14) %>%
mutate(RG1 = row_number()) %>%
left_join(data)

With this code, you will get unique rows based on Kt_15, agegroup, hosp_14, and male. Then a row_number() will be linked to each of the aforementioned data groups. After doing that RG1 is now assigned based on these groupings.

This is untested as you have not provided data.

akash87
  • 3,876
  • 3
  • 14
  • 30