0

I have a dataframe in which there are multiple factor columns, each of which is coded with the response "1" or "2" for "Yes" or "No", respectively. Each column encodes a single level of a 5-level factor as follows:

enter image description here

I want to use dplyr to combine the columns into a single factor column, with priority for the highest level for each case/row, so for this df it would be:

enter image description here

I've tried using mutate and chained if_else statements in dplyr to create a new column but so far haven't been able to do it. Frustratingly this is something I can do easily in Excel with nested IF statements! Many thanks in advance for any help.

donm79
  • 31
  • 1
  • 3
  • Please add data using `dput` and not as images. Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Jul 31 '20 at 04:18
  • It's essentially finding a maximum/minimum per row, so `max.col(-dat, "last")` as per https://stackoverflow.com/questions/17735859/for-each-row-return-the-column-name-of-the-largest-value/17735894 – thelatemail Jul 31 '20 at 04:18
  • I think you want to find last 1 in each row, so you could use `max.col(df == 1, ties.method = 'last')` provided each row has atleast one 1. – Ronak Shah Jul 31 '20 at 04:22

0 Answers0