0

I have a dataframe df like this: enter image description here

I want to add a new column which shows the name of the max value among 5 variables. For example, for the first row: the max value is 5, the name of max value is Olfactory. Similarly repeat for other rows. The expected dataframe should be: enter image description here

My code is:

find_max<-apply(across(.cols=c(12:16),MARGIN=1), FUN=max)
Error: `across()` must only be used inside dplyr verbs.

Is there idea to fix this error? thank you in advance!

Jasmine N
  • 91
  • 7
  • 1
    `apply` doesn't have `across`. You need `apply(df[12:16], 1, FUN = max)` or can use `do.call(pmax, df[12:16])` – akrun Jun 01 '21 at 22:10
  • Related/Possible duplicate https://stackoverflow.com/questions/17735859/for-each-row-return-the-column-name-of-the-largest-value – Ronak Shah Jun 02 '21 at 03:38

1 Answers1

2
   df$Main_Mode <- names(df)[12:16][max.col(df[12:16])]
Onyambu
  • 67,392
  • 3
  • 24
  • 53