1

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)
Thirst for Knowledge
  • 1,606
  • 2
  • 26
  • 43
  • So what would the result of your second condition be? It is not entirely clear – Sotos May 16 '16 at 09:19
  • I want to allocate the "5000" also based on that condition, so something like 'df$new.variable[all.premises < 15000 & which.max(premises<2k, premises>2k)] <- "5000"' – Thirst for Knowledge May 16 '16 at 09:22

1 Answers1

0

I am not sure as to why or how you want to create a new variable based on those conditions, but applying the following two steps we can replace the values,

df$all.premises[df$all.premises < 15000] <- 5000
df[3:4] <- t(apply(df[3:4], 1, function(i) replace(i, which.max(i), 5000)))
df
#    Postcode all.premises premises.2k premises.2k.1
#1  AB1 123        24000        5000          6000
#2  AB2 124        30000        5000          8000
#3  AB3 125         5000        4000          5000
#4  AB4 126        24000        5000          6000
Sotos
  • 51,121
  • 6
  • 32
  • 66
  • 1
    This worked, thanks. I was able to allocate a value using the second line of code and then subset those rows that met that value. What does the 't' mean on the second line before the bracket? – Thirst for Knowledge May 17 '16 at 08:59
  • `t` is for transposing the output from `apply`. [This question](http://stackoverflow.com/questions/9521260/why-apply-returns-a-transposed-xts-matrix) will give you more inside as to why we need to transpose it. – Sotos May 17 '16 at 09:03