How to create a function in R

Asked

Viewed 2,746 times

3

I am wanting to create a function to control the students' grades. I am trying the following function:

note_eng<-read.csv2("note.csv",header=TRUE)

note_eng$Média<-(note_eng[,1]+note_eng[,2]+note_eng[,3])/3

for(i in 1:length(note_eng$Média)){
    if(note_eng$Média[i]>=7) 
    note_eng$Resultado[i]="Aprovado" 
else
    note_eng$Resultado[i]="Reprovado"
}

But what I would like is this:

Data frame format.:

Registration 1st Event 2nd Event 3rd Event Average Event Final Average Final

1º) If the note of each proof is greater than or equal to 7 the color of the text would be blue while otherwise the color of the text would be red

2º) Average the three tests;

3º) Generate a new column where:
If the average is greater than or equal to 7 the result would be Approved (if possible color of the blue text) and in this case (average>=7) already put the result in the column Final Mean If the average is greater than four and less than 7 the result would be Final Proof (black text color and yellow background)
If the average is less than four the result would be Failed (red text color)

4º) In case the student goes to the Final Test, calculate the Final Average in which (Média*0,6)+(Prova Final*0,4) and if the note is higher than 5 the result is Approved (blue text color) otherwise Disapproved (red text color) and put the result in the Final Media column

Someone would know how to do that?

1 answer

4


Before responding, a few considerations:

  1. I recommend not using variables with accents, you may have problem with encoding.
  2. You can calculate the average with the function mean(), or as I will show below, rowMeans() instead of adding it all up and splitting it up.
  3. There’s no way to put text in blue unless you’re going to make an image. The files. csv has no formatting of this type, and I think it is not worth saving in other formats (like .xlsx) just for that reason.

About the code:

Here is an option very step-by-step. As you will surely have to adapt, I left so to show the idea.

notas <- data.frame(Mat=1:4, P1=c(8, 2, 5, 6), P2=c(9, 3, 7, 4), P3=c(10, 4, 6, 7))
notas$media <- rowMeans(notas[,2:4])
notas$resultado <- cut(notas$media, breaks=c(0, 4, 7, 10), labels=c("Reprovado", "PFinal", "Aprovado"))
notas$pfinal <- c(NA, NA, 8, 2)
notas$media.final <- notas$media*0.6+notas$pfinal*0.4
notas$resultado.final <- NA
notas[which(notas$media.final>=5),]$resultado.final <- "Aprovado"
notas[which(notas$media.final<5),]$resultado.final <- "Reprovado"
notas[which(notas$resultado=="Aprovado"),]$resultado.final <- "Aprovado"
notas[which(notas$resultado=="Reprovado"),]$resultado.final <- "Reprovado"
notas

Note that you don’t need to use loops. The function cut() transforms a numeric variable (media) in categorical variables (labels) in accordance with past limits (breaks). You can also find a way to use it to calculate the final result, perhaps by replacing the NA values with 0 or 10 for those who fail or approve, respectively.

  • Some additional information: In the example I invented the grades of four students, one for each possible case. The first was approved direct, the second failed direct, and the other two went to Pfinal, the first approved and the second failed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.