How to create a function in R

3

I want to create a function to control students' grades. I'm 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 would be the following:

Data.frame format:

Enrollment 1st Test 2nd Test 3rd Test Average Final Final Final Test

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º) Calculate the average of the three tests;

3rd) Generate a new column in which:
If the mean is greater than or equal to 7 the result would be Approved (if possible blue text color) and in this case (mean > = 7) already put the result in the Final Average column If the mean 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 Disapproved (red text color)

4º) If the student goes to the Final Test, calculate the Final Average in which% with% and if the mark is greater than 5 the result is Approved (blue text color) otherwise Failed (red text color ) and put the result in the Final Average column

Would anyone know how to do this?

    
asked by anonymous 21.03.2015 / 22:26

1 answer

4

Before answering, some considerations:

  • I recommend not using variables with accents, you may have problem with encoding .
  • You can calculate the average with the function mean() , or as I will show below, rowMeans() instead of adding everything and dividing.
  • You can not put text in blue, unless you go to make an image. The .csv files are not formatted like this, and I do not think it's worth saving in other formats (like .xlsx) just for that.
  • About the code:

    Here is a very step-by-step option. As you will certainly have to adapt, I left it 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 do not need to use loops. The cut() function transforms a numeric variable ( media ) into categorical variables ( labels ) according to the passed limits ( breaks ). You can look for a way to use it also for calculating the final result, perhaps by replacing the NA values by 0 or 10 for those who fail or fail, respectively.

        
    22.03.2015 / 03:45