I want to allocate a value based on multiple conditions which include (i) all values below 15,000, and (ii) the highest value for each row across two columns. So far I have the following code which creates a new variable and allocates "5000" to those rows meeting the first condition.
df$new.variable[all.premises < 15000] <- "5000"
How do I incorporate a second condition, which works out which the highest value is across the other two variables (called premises<2k & premises>2k)? Here's the data structure:
Postcode all.premises premises<2k premises>2k
AB1 123 24000 18000 6000
AB2 124 30000 22000 8000
AB3 125 12000 4000 8000
AB4 126 24000 18000 6000
I think which.max might be of use here, in which case it could be something like:
df$new.variable[all.premises < 15000 & which.max(premises<2k, premises>2k)] <- "5000"
To summarise the two conditions:
- I want to allocate different values to those rows with premises over or under a certain number (using all.premises) (this is straight forward)
- I also want to allocate different values to those rows which have the majority of premises either more or less then 2km from a specific location (suing premises<2k and premises>2k)