How can I extract column value corresponding to maximum row value in R? -
this question has answer here:
i need calculate : a) maximum , minimum (temperature) values every row of data set(200 such rows). b) every column corresponds 'jan', 'feb' ... 'dec' (12 columns). thus, need find month associated maximum , minimum temperature.
for (a) works:
i= 1 temp.max = null for(i in 1:200) { temp.max[i]<- max(temp.data[i,1:12])}
can please me (b)?
eg. if dataset looks :
jan feb mar apr may jun ... dec 1 2 3 4 12 6 2
max value 12. need output that, 'may' corresponding month.
max.col
handy (i've repeated first row 3 times show works multiple rows):
names(dat)[max.col(dat,ties.method="first")] #[1] "may" "may" "may"
data used:
dat <- setnames(data.frame(matrix(c(1,2,3,4,12,6,2),nrow=1)),month.abb[1:7]) dat <- dat[rep(1,3),]
Comments
Post a Comment