should I use a for loop for this?

I have some data that looks like this:
there are four columns with values, I want to find out for each row, out of three of those columns, which one has the largest value of the three.... the final result that I want is an additional column that shows the character name of the column with the largest value for each row

so a new column that would look like

  ID  type        n_urban  n_rural  n_comp_rural   n_counties
      rural
      mixed
      rural
      urban

Is it best to use a for loop? or should I just do this with ifelse statements?

Thanks!

structure(list(economic_area_id = c(57001, 57002, 57003, 57004, 
57005, 57006, 57007, 57008, 57009, 57010), n_urban = c(2, 5, 
8, 7, 4, 0, 15, 6, 4, 2), n_rural = c(2, 2, 13, 6, 2, 8, 3, 6, 
6, 10), n_comp_rural = c(9, 5, 6, 1, 0, 3, 9, 16, 1, 2), n_counties = c(13, 
12, 27, 14, 6, 11, 27, 28, 11, 14)), class = c("tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -10L))

Nevermind, I did this with a nested ifelse statement

EA$type <- ifelse(EA$n_urban > EA$n_comp_rural & EA$n_urban > EA$n_rural, "mostly urban",
                  ifelse(EA$n_rural > EA$n_urban & EA$n_rural > EA$n_comp_rural, "mostly rural",
                         ifelse(EA$n_comp_rural > EA$n_rural & EA$n_comp_rural > EA$n_urban, "very rural",
                                ifelse(EA$n_urban==EA$n_rural | EA$n_rural==EA$n_comp_rural | EA$n_urban==EA$n_comp_rural, "mixed", ""))))


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