0

I have a test done several times and would like to create a column with the worst performance. A simplified version of the data data would look like

> df <- structure(list(Exam1 = c("excellent", "excellent", "good", "average", "poor", "good", "good", "poor"), Exam2 = c(NA, NA, "good", NA, NA, NA, "excellent", "poor"), Exam3 = c(NA, NA, NA, NA, NA, "excellent", "excellent", NA), Exam4 = c("excellent",NA,"excellent", "excellent", "poor", NA, "excellent", "poor"), Exam5 = c(NA, NA, "excellent", NA, "good", NA, "excellent", NA), Exam6 = c(NA, NA, NA, "good", NA, NA, "excellent", NA)), .Names = c("Exam1", "Exam2", "Exam3", "Exam4", "Exam5", "Exam6"), row.names = c(NA, 8L), class = "data.frame")



     Exam1     Exam2     Exam3     Exam4     Exam5     Exam6
1 excellent      <NA>      <NA> excellent      <NA>      <NA>
2 excellent      <NA>      <NA>      <NA>      <NA>      <NA>
3      good      good      <NA> excellent excellent      <NA>
4   average      <NA>      <NA> excellent      <NA>      good
5      poor      <NA>      <NA>      poor      good      <NA>
6      good      <NA> excellent      <NA>      <NA>      <NA>
7      good excellent excellent excellent excellent excellent
8      poor      poor      <NA>      poor      <NA>      <NA>

I would like to add a column with the worst test results

     Exam1     Exam2     Exam3     Exam4     Exam5     Exam6 WorstExam
1 excellent      <NA>      <NA> excellent      <NA>      <NA> excellent
2 excellent      <NA>      <NA>      <NA>      <NA>      <NA> excellent
3      good      good      <NA> excellent excellent      <NA>      good
4   average      <NA>      <NA> excellent      <NA>      good   average
5      poor      <NA>      <NA>      poor      good      <NA>      poor
6      good      <NA> excellent      <NA>      <NA>      <NA>      good
7      good excellent excellent excellent excellent excellent      good
8      poor      poor      <NA>      poor      <NA>      <NA>      poor
  • Please add a reproducible example, and ideally an expected result as it stands it is too hard to understand your question – Bruno May 11 '20 at 18:07
  • Hi user13519472, welcome to Stack Overflow. It will be much easier to help if you provide at least a sample of your data with `dput(dput)` or if your data is very large `dput([1:20,])`. You can edit your question and paste the output. You can surround it with three backticks (```) for better formatting. Additionally as @Bruno suggests, please provide your expected output. See [How to make a reproducible example](https://stackoverflow.com/questions/5963269/) for more info. – Ian Campbell May 11 '20 at 20:59
  • Hi user*, a good rule of thumb is to provide a subset of your data as recommended by Ian, the code that you attempted, and what the data frame should look like when the solution is made. That way someone can cut and paste the data and code into their environment, and work out a solution and share the code back with you. Thanks :) – mysteRious May 11 '20 at 22:15
  • Thank you for your answers sorry for being unclear. – user13519472 May 15 '20 at 14:38
  • Thank you for your answers, sorry for being unclear. I edited the question I hope it is clearer now – user13519472 May 15 '20 at 14:45
  • Hi @user13519472, I voted to reopen, but I doubt it will get enough votes. One approach is with a category vector and `apply`. `categories <- setNames(1:4,c("poor", "average", "good", "excellent"))` and `df$WorstExam <- apply(df,1,function(x){names(categories[min(categories[names(categories) %in% x])])})` should take care of it. – Ian Campbell May 15 '20 at 20:06

0 Answers0