My goal here is to load an Excel file from a Doodle poll and then find an optimal pair of times for an event (i.e find a pair of times that is best the highest number of people).
library("readxl")
#we might not need the first 6 rows
doodle <- read_excel("Doodle.xls", col_names = FALSE, skip = 6)
#skip the last row and first column
m <- nrow(doodle)
n <- ncol(doodle)
just_the_results <- doodle[1:m-1, 2:n]
#convert "OK" to ones
just_the_results[just_the_results == "OK"] <- 1
#replace missing values with zeroes
just_the_results[is.na(just_the_results)] <- 0
#make n-by-n matrix to hold the sums
m <- nrow(just_the_results)
n <- ncol(just_the_results)
sums <- matrix(0, nrow = n, ncol = n) #initialize zero matrix
for(j in 1:n){
for(k in 1:n){
if(j < k){
#the "OR" logical vector operation is key
sums[j,k] <- sum(as.numeric(unlist(just_the_results[,j])) |
as.numeric(unlist(just_the_results[,k])) )
}
}
}
the_desired_columns <- as.numeric(which(sums == max(sums), arr.ind = TRUE))
The optimal pair of times are in columns 6 and 12 of the Doodle poll, which would be available for 20 out of the 26 poll participants.