-
Having some difficulty with an if/else set of conditions. The conditions evaluate properly for 3 out of the 4 conditions. I please need some other eyes on what's possibly going wrong. The overall gist is this. I have four treatments (1 - 4) and three treatment comparisons. The if/else statements evaluate which comparison and which treatment(s) should be dealt with in a given comparison. I've put the code below - it's just text to track the if/else behavior, so you can copy/paste/run this yourself without the need for any files or anything. # Vector of treatments
treatments <- c(1:4)
# Vector of comparisons
comparisons <- c("all", "f_v_m", "c_v_e")
for (comparison in comparisons) {
# Create data frame with coefficients of variation for each Treatment group
for (treatment in treatments) {
# Print statements to track loop progress
print(paste("evaluating", comparison, sep = " "))
print(paste("treatment is", treatment, sep = " "))
if (comparison == "all") {
print(paste("Working on comparison: ", comparison, sep = " "))
print("")
}
# Treatments 2&4 (females)
else if (comparison == "f_v_m" && treatment == "2" || treatment == "4") {
print("Made it to the females!")
print(paste("Working on comparison: ", comparison, "and treatment", treatment, sep = " "))
print("")
} # Treatments 1&3 (males)
else if (comparison == "f_v_m" && treatment == "1" || treatment == "3") {
print("Made it to the males!")
print(paste("Working on comparison: ", comparison, "and treatment", treatment, sep = " "))
print("")
} # Treatments 1&2 (control)
else if (comparison == "c_v_e" && treatment == "1" || treatment == "2") {
print("Made it to the controls!")
print(paste("Working on comparison: ", comparison, "and treatment", treatment, sep = " "))
print("")
} # Treatments 3&4 (exposed)
else if (comparison == "c_v_e" && treatment == "3" || treatment == "4") {
print("Made it to exposed!")
print(paste("Working on comparison: ", comparison, "and treatment", treatment, sep = " "))
print("")
}
}
}
OUTPUT: [1] "evaluating all"
[1] "treatment is 1"
[1] "Working on comparison: all"
[1] ""
[1] "evaluating all"
[1] "treatment is 2"
[1] "Working on comparison: all"
[1] ""
[1] "evaluating all"
[1] "treatment is 3"
[1] "Working on comparison: all"
[1] ""
[1] "evaluating all"
[1] "treatment is 4"
[1] "Working on comparison: all"
[1] ""
[1] "evaluating f_v_m"
[1] "treatment is 1"
[1] "Made it to the males!"
[1] "Working on comparison: f_v_m and treatment 1"
[1] ""
[1] "evaluating f_v_m"
[1] "treatment is 2"
[1] "Made it to the females!"
[1] "Working on comparison: f_v_m and treatment 2"
[1] ""
[1] "evaluating f_v_m"
[1] "treatment is 3"
[1] "Made it to the males!"
[1] "Working on comparison: f_v_m and treatment 3"
[1] ""
[1] "evaluating f_v_m"
[1] "treatment is 4"
[1] "Made it to the females!"
[1] "Working on comparison: f_v_m and treatment 4"
[1] ""
[1] "evaluating c_v_e"
[1] "treatment is 1"
[1] "Made it to the controls!"
[1] "Working on comparison: c_v_e and treatment 1"
[1] ""
[1] "evaluating c_v_e"
[1] "treatment is 2"
[1] "Made it to the controls!"
[1] "Working on comparison: c_v_e and treatment 2"
[1] ""
[1] "evaluating c_v_e"
[1] "treatment is 3"
[1] "Made it to the males!"
[1] "Working on comparison: c_v_e and treatment 3"
[1] ""
[1] "evaluating c_v_e"
[1] "treatment is 4"
[1] "Made it to the females!"
[1] "Working on comparison: c_v_e and treatment 4"
[1] "" The problem part is this, where the if/else never enters: # Treatments 3&4 (exposed)
else if (comparison == "c_v_e" && index == "3" || index == "4") {
print("Made it to exposed!")
print(paste("Working on comparison: ", comparison, "and index", index, sep = " "))
print("") This is despite the fact that the print statements show that the proper conditions exist!: [1] "evaluating c_v_e"
[1] "treatment is 3"
[1] "Made it to the males!"
[1] "Working on comparison: c_v_e and treatment 3" However, from this line # Treatments 1&3 (males)
else if (comparison == "f_v_m" && treatment == "1" || treatment == "3") {
print("Made it to the males!")
print(paste("Working on comparison: ", comparison, "and treatment", treatment, sep = " "))
print("") Does anyone happen to have any suggestions? I'm completely unable to wrap my head around what's happening and why the code never enters the last |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Serendipitously, got this functional code during the SAFS monthly DEI community meeting from @lisannepetracca! Thanks so much! # Vector of treatments
treatments <- c(1:4)
# Vector of comparisons
comparisons <- c("all", "f_v_m", "c_v_e")
for (comparison in comparisons) {
# Create data frame with coefficients of variation for each Treatment group
for (treatment in treatments) {
# Print statements to track loop progress
print(paste("evaluating", comparison, sep = " "))
print(paste("treatment is", treatment, sep = " "))
if (comparison == "all") {
print(paste("Working on comparison: ", comparison, sep = " "))
print("") } else if (comparison == "f_v_m" & (treatment == 2 | treatment == 4)) {
print("Made it to the females!")
print(paste("Working on comparison: ", comparison, "and treatment", treatment, sep = " "))
print("") } else if (comparison == "f_v_m" & (treatment == 1 | treatment == 3)) {
print("Made it to the males!")
print(paste("Working on comparison: ", comparison, "and treatment", treatment, sep = " "))
print("")} else if (comparison == "c_v_e" & (treatment == 1 | treatment == 2)) {
print("Made it to the controls!")
print(paste("Working on comparison: ", comparison, "and treatment", treatment, sep = " "))
print("")} else if (comparison == "c_v_e" & (treatment == 3 | treatment == 4)) {
print("Made it to exposed!")
print(paste("Working on comparison: ", comparison, "and treatment", treatment, sep = " "))
print("")
}}} Per my communications with her, this was primary problem:
|
Beta Was this translation helpful? Give feedback.
Serendipitously, got this functional code during the SAFS monthly DEI community meeting from @lisannepetracca! Thanks so much!