zone_id=1:6
v1=c(12,22,31,12,5,17)
v2=c(15,22,28,16,18,21)
v3=c(18,10,14,9,10,17)
v4=c(20,3,2,5,12,21)
mydata=data.frame(zone_id,v1,v2,v3,v4)
I have a dataframe a crude model of which can be made using the above code. It consists of rows of data relating to geographical areas. I have variables (4 in this example, but 69 in my actual dataset) which contain integers which are observations in those areas. For each zone_id I want to identify the variable from V1 to V4 which contains the maximum value. Where there is a tie I want to return the names of each of the variables that contain the tied maximum value. So for zone 1, I want to return V4, for zone 2 I want to return V1 and V2 and so on.
I am very new to R and have been unable to get to first base with this. I've explored R help files and thought there may be a solution using sweep? Any help appreciated.
One method is to use
rank
. Note the minus sign before the data as the default ordering is low->highAnd you could extract the names with
sapply
: