Created
November 14, 2012 15:41
-
-
Save lordsutch/4072848 to your computer and use it in GitHub Desktop.
Add a variable to a data frame giving a mean of a set of quiz grades, dropping n grades
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Note: we're assuming each grade should be weighted equally here. | |
dropmean <- function(x, drop=2) { | |
count <- length(x)-drop | |
round(sum(sort(x, decreasing=TRUE)[seq(1, count)], na.rm=T)/count, 2) | |
} | |
## Same formula for multiple sections; note list.files uses a regex pattern, not a glob pattern | |
infiles <- list.files(pattern='^gradebook-quizzes-[0-9]+\\.csv$') | |
for(filename in infiles) { | |
dat <- read.csv(filename) | |
newdat <- dat[,1:3] | |
quizzes <- dat[,grep('^Quiz...Chapter.([0-9]|1[012])$', colnames(dat))] | |
## Quiz grades are out of 10, so I multiplied here by 10 to make it a percentage. | |
newdat$DroppedAverage <- dat$DroppedAverage <- apply(quizzes, 1, dropmean)*10 | |
str(dat) | |
write.csv(newdat, paste("fixed", filename, sep='-'), row.names=FALSE) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment