Wednesday, March 11, 2015

R code, midterm grading, for sections, with letter grade assignment

file = "gradebio125,20150310.R"

#bio125 grades
# Mid-Semester Exam   25%
# Final Exam 25%
# Assignments 25%
# Project Report 10%
# Presentation    10%
# Class Participation     5%

rm(list=ls())
list.files()
flag = 2

#The - signs have to be replaced with zeros in textwrangler
infile = "201501-61953-01 Grades.csv"
if( flag == 2){ infile = "201501-61954-02 Grades.csv" }

tb = read.csv(infile)

empty.columns= NULL
for (j in 7:length(tb[1,])){
 tb[,j] = as.numeric( tb[,j])
 tb[is.na(tb[,j]),j] = 0
 if( max(tb[,j])==0 ) { empty.columns = c(empty.columns, j)}
}
str(tb)
tb2 = tb[, - empty.columns]
summary(tb)

names(tb2)[ grep("total", names(tb2)) ]
tb2 = tb2[, -grep("total", names(tb2)) ]
tb2 = tb2[, -grep("assessment", names(tb2)) ]
names(tb2);
sort(names(tb2))
tb3 = tb2[, sort(names(tb2))]

####
#pick highest scores from regular and makeup ones
makeups = grep("make", names(tb3))
regulars = makeups - 1
names(tb3)[c(makeups, regulars)]
for (i in makeups){
  tb3[,i-1]= apply(tb3[,c(i-1,i)], 1, max)
}
tb3[, c(makeups[2]-1, makeups[2])] #passed.
# now, remove makeups
tb4 = tb3[, -makeups]
###end of pick highest grades

##here is the output report
out= tb4[,c("First.name","Last.name")]

#Midterm 25%
names(tb4)[ grep( 'mid', names(tb4)) ]
#section 1
Midterm = c(  "Quiz.Spring.2015..section.1..midterm..open.part", "X2015midterm.closed.book"  )
#section 2  
if( flag==2){ Midterm = c(  "Quiz.Sp15..Section2..midterm..open.book.part", "X2015.midterm.exam.closed.book" )  }
out$Exam = apply( tb4[,Midterm], 1, sum)
out$Exam = out$Exam + 10  #adjust midterm exam

#Final 25%
# ....

# Project Report  10%
names(tb4)[ grep( 'port', names(tb4)) ]
Reports = c("Assignment.GoogleDoc.report.of.RE.digestion.lab..group.submission.",    
  "Assignment.Set.up.GoogleDoc.for.final.project.and.report.of.miniprep.lab"
)

# Class Participation     5%
#names(tb4)[ grep( 'note', names(tb4)) ]
names(tb4)[ grep( 'Quiz.lab', names(tb4)) ]
Participation = names(tb4)[ grep( 'Quiz', names(tb4)) ]
Participation = Participation [-grep("lab", Participation )]
#Participation = Participation[-grep("Quiz.Spring.2015..section.1..midterm..open.part", Participation)]
Participation = Participation[-grep(Midterm, Participation)]

out$Participation = apply(tb4[,Participation], 1, sum)
out$Participation = 5 * out$Participation / max(out$Participation)

#Assignments  #25%
Assignments = names(tb4)[ grep("Quiz", names(tb4)) ]
out$Assignments = apply( tb4[,Assignments], 1, sum)
out$Assignments = 25 * out$Assignments / max( out$Assignments )

# Presentation    10%
# ...


#out$exam = tb4[,Midterm]

head(out)
out$total = apply( out[,3:5], 1, sum)
out$FinalGrade =  100*out$total/ (50+25+5)
hist(out$FinalGrade, br=20)
summary(out$Exam*2)

grade2letter = function(x){
  if(x>94){    ret='A'
  }else if (x >90) {    ret='A-'
  }else if (x >87 ){    ret = 'B+'
  }else if (x > 84){    ret = 'B'
  }else if (x >80){    ret = 'B-'
  }else if (x > 76){  ret = 'C+'
  }else if (x > 70){ ret = 'C'
  }else if (x > 67){ ret = 'C-'
  }else if (x > 64){ ret = 'D+'
  }else if (x > 60){ ret = 'D'
  }else {   ret = 'F'
  }
  return (ret)
}
grade2letter(70); grade2letter(88)

out$letter = unlist(lapply(out$FinalGrade,  grade2letter))

outfile = paste( "out", infile, sep="." )
write.csv(out, outfile )

#generate a sorted report
out.sorted = out[order(out$FinalGrade),]
write.csv(out.sorted, file=paste("sorted.out",infile, sep="."))

#q("no")
#################


No comments:

Post a Comment