Search code examples
rfunctionapplysapply

Creating a function to generate a matrix using several lists


Suppose I have the following lists:

brice_grades <- list(math = c(90, 64, 100), reading = c(55, 71, 50),
                 science = c(100, 100, 98))
talia_grades <- list(math = c(70, 64, 70), reading = c(100, 80, 50),
                     science = c(100, 92, 98))
annie_grades <- list(math = c(14, 64, 50), reading = c(90, 71, 99),
                     science = c(88, 70, 98))

I am trying to create a function, report_card, that I can run through the sapply function such that I return a matrix with average grades for each class, unless the grade is below a 70, in which case it says "fail" rather than the grade.

Here is what I mean:

          brice_grades  talia_grades  annie_grades
math      84.7          fail          fail
reading   fail          76.7          86.7
science   99.3          96.7          85.3

I'm new to creating functions in R so I'm a little tripped up. I'm not sure if I should be using sapply given that all of my output is not the same class ("fail" is a string and the means are numeric obviously). Additionally, it gets a little weird for me writing this function using vectors contained within lists.


Solution

  • If you're happy with a character matrix output, a double loop with sapply should take care of it.

    failfun <- function(x) {m <-  mean(x); if(m < 70) "fail" else round(m,1)}
    sapply(mget(c("brice_grades", "talia_grades", "annie_grades")), \(x) sapply(x, failfun))
    #        brice_grades talia_grades annie_grades
    #math    "84.7"       "fail"       "fail"      
    #reading "fail"       "76.7"       "86.7"      
    #science "99.3"       "96.7"       "85.3"