I have a dataframe where I am trying to find the column name which holds the minimum value for a row.

ColA ColB ColC ColD (I want a column here which identifies column name with min. value:)

1 2 3 5 answer = ColA

9 2 1 5 answer = ColC

7 2 3 5 answer = ColB

6 2 3 5 answer = ColB

4 2 1 5 answer = ColC

I am using a function:

df$answer <- names(df)[which.min(apply(df,MARGIN=2,min))]

however, this function selects the answer ColA in all cases and does not seem to apply to each row but the entire data frame.

Any R experts who know how to solve this issue?

thank you