Skip to content

Instantly share code, notes, and snippets.

@MattSandy
Created April 4, 2016 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattSandy/3a87b59ea354edb4e16f83874187ff5a to your computer and use it in GitHub Desktop.
Save MattSandy/3a87b59ea354edb4e16f83874187ff5a to your computer and use it in GitHub Desktop.
Calculate percentage of occurrences for diseases based on the herd and date.
df <- read.csv(paste("~/R/df/","import.csv",sep=""),
header = TRUE, sep = ",", quote = "\"",stringsAsFactors = FALSE, encoding="UTF-8")
names(df) <- c("herd","date","disease")
output <- matrix(, nrow = 0, ncol = 4)
for(herd in unique(df$herd)) {
for(date in unique(df$date)) {
for(disease in unique(df$disease)) {
subset <- df[which(df$herd==herd & df$date==date),]
output <- rbind(output,c(herd,date,disease,nrow(subset[which(subset$disease==disease),])/nrow(subset)))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment