I am new to R and have am having trouble understanding how to substitute a variable name for column name. For example, the code below works fine finding the row with the max value for col2 with defined column names
> df<-data.frame(name=sample(LETTERS[1:5]),val1=sample(5), val2=sample(5))
> df
name val1 val2
1 B 3 1
2 C 4 3
3 E 5 2
4 D 2 4
5 A 1 5
> df[which(df$val2 == max(df$val2)),]
5 A 1 5
But if I cannot seem to get this work with a string variable for the column name such as
colName = "val2"