R interaction.plot - r

I want to draw an interaction.plot for my data.
with(data, interaction.plot(biofeedback, diet, as.numeric(drug),
xlab="Biofeedback", ylab="Drug", ylim = c(0, 4), col = 2:3))
Result:
But clearly that's not correct because my data is:
## biofeedback diet drug pressure
## 1 present absent 1 170
## 2 present absent 1 175
## 3 present absent 1 165
...
## 7 present present 1 161
## 8 present present 1 173
## 9 present present 1 157
...
## 19 present present 2 164
## 20 present present 2 166
## 21 present present 2 159
...
This data has different drugs for absent and present Biofeedback for present diet. And it has some drugs for absent diet. None of this shows on the plot. Why?
dput(data)
## structure(list(biofeedback = structure(c(2L, 2L, 2L, 2L, 2L,
## 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
## 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
## 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
## 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
## 1L, 1L, 1L), .Label = c("absent", "present"), class = "factor"),
## diet = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
## 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
## 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
## 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
## 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
## 2L, 2L), .Label = c("absent", "present"), class = "factor"),
## drug = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
## 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
## 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L,
## 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
## 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
## 3L, 3L), .Label = c("1", "2", "3"), class = "factor"), pressure = c(170L,
## 175L, 165L, 180L, 160L, 158L, 161L, 173L, 157L, 152L, 181L,
## 190L, 186L, 194L, 201L, 215L, 219L, 209L, 164L, 166L, 159L,
## 182L, 187L, 174L, 180L, 187L, 199L, 170L, 204L, 194L, 162L,
## 184L, 183L, 156L, 180L, 173L, 173L, 194L, 197L, 190L, 176L,
## 198L, 164L, 190L, 169L, 164L, 176L, 175L, 189L, 194L, 217L,
## 206L, 199L, 195L, 171L, 173L, 196L, 199L, 180L, 203L, 202L,
## 228L, 190L, 206L, 224L, 204L, 205L, 199L, 170L, 160L, 179L,
## 179L)), .Names = c("biofeedback", "diet", "drug", "pressure"
## ), row.names = c(NA, -72L), class = "data.frame")

It looks like there is no error. The average for each group is 2.0, which is what the plot shows.
> df$drug <- as.numeric(levels(df$drug))[df$drug]
> library(plyr)
> ddply(df, .(biofeedback, diet), summarise, val = mean(drug))
biofeedback diet val
1 absent absent 2
2 absent present 2
3 present absent 2
4 present present 2

Related

ggplot2 add breaks/intervals to discrete variable in an animated plot

Context: I'm trying to make an animated line plot where I compare the levels of Resting Blood Pressure (Y variable - The graph says cholesterol, but it should say Resting Blood Pressure) as the Age increases (x variable) in function of the patient having heart disease or not (0 for no heart disease and 1 for heart disease).
The plot has a lot of "spikes," so I'd like to create some classes/breaks to level things out a bit, but I'm not sure how I could do it given that age is a discrete variable. I was trying to add something like scale_x_discrete() and then adding a sequence like seq(10,80,10) but it didn't work. I was also taking a look at using cut with Sturges rules, but once again that's used for continuous variables.
This is the code:
p <- ggplot(
df,
aes(Age, RestingBP, group = HeartDisease, color = HeartDisease)
) +
geom_line() +
scale_color_viridis_d() +
scale_alpha_manual(values=classes) +
labs(x = "Age", y = "Cholesterol") +
theme(legend.position = "top")
p + transition_reveal(Age)
This is the graph:
Thank you in advance!
Some data:
structure(list(Age = c(40L, 49L, 37L, 48L, 54L, 39L, 45L, 54L,
37L, 48L, 37L, 58L, 39L, 49L, 42L, 54L, 38L, 43L, 60L, 36L, 43L,
44L, 49L, 44L, 40L, 36L, 53L, 52L, 53L, 51L, 53L, 56L, 54L, 41L,
43L, 32L, 65L, 41L, 48L, 48L, 54L, 54L, 35L, 52L, 43L, 59L, 37L,
50L, 36L, 41L), Gr_etario = structure(c(2L, 2L, 1L, 2L, 2L, 1L,
2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 3L, 1L, 2L, 2L,
2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 3L, 2L,
2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L), .Label = c("Menos de 40 anos",
"Entre 40 e 60 anos", "Acima de 60 anos"), class = "factor"),
Sex = structure(c(2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L,
1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L,
2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("F",
"M"), class = "factor"), ChestPainType = structure(c(2L,
3L, 2L, 1L, 3L, 3L, 2L, 2L, 1L, 2L, 3L, 2L, 2L, 1L, 3L, 2L,
1L, 2L, 1L, 2L, 4L, 2L, 2L, 2L, 3L, 3L, 1L, 2L, 2L, 2L, 3L,
3L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 3L, 2L, 3L, 1L, 3L,
1L, 2L, 3L, 1L), .Label = c("ASY", "ATA", "NAP", "TA"), class = "factor"),
RestingBP = c(140L, 160L, 130L, 138L, 150L, 120L, 130L, 110L,
140L, 120L, 130L, 136L, 120L, 140L, 115L, 120L, 110L, 120L,
100L, 120L, 100L, 120L, 124L, 150L, 130L, 130L, 124L, 120L,
113L, 125L, 145L, 130L, 125L, 130L, 150L, 125L, 140L, 110L,
120L, 150L, 150L, 130L, 150L, 140L, 120L, 130L, 120L, 140L,
112L, 110L), Cholesterol = c(289, 180, 283, 214, 195, 339,
237, 208, 207, 284, 211, 164, 204, 234, 211, 273, 196, 201,
248, 267, 223, 184, 201, 288, 215, 209, 260, 284, 468, 188,
518, 167, 224, 172, 186, 254, 306, 250, 177, 227, 230, 294,
264, 259, 175, 318, 223, 216, 340, 289), FastingBS = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = c("0", "1"), class = "factor"),
RestingECG = structure(c(2L, 2L, 3L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 3L,
3L, 2L, 2L, 3L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("LVH",
"Normal", "ST"), class = "factor"), MaxHR = c(172L, 156L,
98L, 108L, 122L, 170L, 170L, 142L, 130L, 120L, 142L, 99L,
145L, 140L, 137L, 150L, 166L, 165L, 125L, 160L, 142L, 142L,
164L, 150L, 138L, 178L, 112L, 118L, 127L, 145L, 130L, 114L,
122L, 130L, 154L, 155L, 87L, 142L, 148L, 130L, 130L, 100L,
168L, 170L, 120L, 120L, 168L, 170L, 184L, 170L), ExerciseAngina = structure(c(1L,
1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L,
1L, 1L, 1L, 1L), .Label = c("N", "Y"), class = "factor"),
Oldpeak = c(0, 1, 0, 1.5, 0, 0, 0, 0, 1.5, 0, 0, 2, 0, 1,
0, 1.5, 0, 0, 1, 3, 0, 1, 0, 3, 0, 0, 3, 0, 0, 0, 0, 0, 2,
2, 0, 0, 1.5, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0), ST_Slope = structure(c(3L,
2L, 3L, 2L, 3L, 3L, 3L, 3L, 2L, 3L, 3L, 2L, 3L, 2L, 3L, 2L,
2L, 3L, 2L, 2L, 3L, 2L, 3L, 2L, 3L, 3L, 2L, 3L, 3L, 3L, 2L,
3L, 2L, 2L, 3L, 3L, 2L, 3L, 3L, 2L, 3L, 2L, 3L, 3L, 2L, 2L,
3L, 3L, 2L, 2L), .Label = c("Down", "Flat", "Up"), class = "factor"),
HeartDisease = structure(c(1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L,
2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L,
2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L,
1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L), .Label = c("0",
"1"), class = "factor")), row.names = c(NA, 50L), class = "data.frame")

Categorical covariate with two way anova in R

This is a random sample of my data set:
structure(list(DTI_ID = structure(c(31L, 241L, 84L, 298L, 185L,
269L, 198L, 24L, 286L, 177L, 228L, 158L, 57L, 293L, 218L, 8L,
180L, 39L, 211L, 134L, 291L, 309L, 99L, 70L, 154L, 138L, 250L,
41L, 276L, 262L, 96L, 139L, 232L, 12L, 294L, 38L, 244L, 289L,
280L, 196L, 58L, 44L, 188L, 152L, 143L, 302L, 201L, 27L, 24L,
67L, 247L, 223L, 74L, 32L, 110L, 98L, 303L, 256L, 71L, 30L, 236L,
266L, 307L, 224L, 100L, 73L, 288L, 230L, 182L, 159L, 190L, 123L,
241L, 169L, 103L, 40L, 248L, 293L, 60L, 260L, 168L, 267L, 144L,
89L, 139L, 231L, 204L, 130L, 278L, 227L, 205L, 268L, 88L, 221L,
208L, 306L, 242L, 145L, 21L, 165L, 217L, 159L, 206L, 70L, 121L,
181L, 95L, 279L, 265L, 4L, 122L, 177L, 234L, 34L, 261L, 86L,
2L, 296L, 39L, 283L, 251L, 126L, 188L, 176L, 220L, 77L, 225L,
73L, 48L, 107L, 280L, 118L, 38L, 310L, 297L, 258L, 89L, 205L,
4L, 54L, 16L, 95L, 119L, 40L, 9L, 66L, 64L, 55L, 131L, 290L,
166L, 170L, 182L, 139L, 125L, 201L, 302L, 137L, 8L, 81L, 61L,
119L, 278L, 135L, 117L, 65L, 21L, 200L, 150L, 146L, 54L, 262L,
152L, 224L, 162L, 111L, 251L, 130L, 41L, 271L, 33L, 86L, 32L,
199L, 49L, 180L, 101L, 271L, 80L, 84L, 293L, 5L, 170L, 74L, 279L,
281L, 255L, 210L, 52L, 248L, 53L, 121L, 190L, 141L, 213L, 138L,
112L, 234L, 235L, 40L, 233L, 115L, 154L, 11L, 76L, 29L, 19L,
249L, 1L, 207L), .Label = c("5356", "5357", "5358", "5359", "5360",
"5363", "5373", "5381", "5383", "5386", "5395", "5397", "5400",
"5401", "5444", "5445", "5446", "5448", "5450", "5451", "5454",
"5472", "5473", "5475", "5476", "5477", "5478", "5480", "5481",
"5483", "5487", "5494", "5495", "5504", "5505", "5506", "5507",
"5508", "5509", "5513", "5514", "5515", "5516", "5517", "5518",
"5519", "5521", "5523", "5524", "5526", "5527", "5528", "5544",
"5545", "5546", "5547", "5551", "5552", "5553", "5554", "5555",
"5558", "5559", "5560", "5562", "5564", "5566", "5573", "5574",
"5575", "5576", "5577", "5578", "5579", "5584", "5585", "5587",
"5588", "5589", "5591", "5594", "5595", "5604", "5611", "5612",
"5613", "5615", "5616", "5619", "5620", "5621", "5622", "5626",
"5627", "5628", "5631", "5632", "5634", "5635", "5643", "5652",
"5653", "5654", "5655", "5656", "5657", "5659", "5660", "5661",
"5664", "5665", "5666", "5669", "5671", "5672", "5673", "5678",
"5680", "5688", "5689", "5690", "5691", "5692", "5698", "5699",
"5700", "5702", "5703", "5704", "5706", "5708", "5709", "5710",
"5730", "5731", "5732", "5733", "5734", "5735", "5739", "5740",
"5741", "5742", "5743", "5744", "5745", "5746", "5747", "5748",
"5749", "5750", "5753", "5754", "5755", "5766", "5767", "5776",
"5777", "5778", "5779", "5780", "5781", "5787", "5788", "5789",
"5790", "5791", "5792", "5793", "5797", "5798", "5799", "5800",
"5801", "5810", "5811", "5812", "5813", "5814", "5819", "5820",
"5821", "5822", "5823", "5824", "5825", "5827", "5828", "5829",
"5830", "5857", "5859", "5874", "5875", "5876", "5877", "5878",
"5879", "5883", "5884", "5886", "5887", "5888", "5889", "5890",
"5892", "5893", "5896", "5899", "5900", "5909", "5910", "5918",
"5919", "5920", "5921", "5922", "5923", "5927", "5929", "5931",
"5932", "5933", "5934", "5936", "5937", "5941", "5943", "5944",
"5949", "5950", "5951", "5952", "5956", "5957", "5958", "5959",
"5971", "5972", "5973", "5976", "5979", "5980", "5981", "6001",
"6002", "6003", "6004", "6005", "6009", "6027", "6028", "6033",
"6042", "6054", "6063", "6067", "6073", "6076", "6077", "6078",
"6079", "6080", "6081", "6082", "6083", "6098", "6102", "6103",
"6104", "6105", "6106", "6107", "6111", "6119", "6133", "6146",
"6147", "6157", "6158", "6160", "6161", "6162", "6163", "6164",
"6165", "6166", "6167", "6168", "6169", "6170", "6171", "6172",
"6173", "6174", "6175", "6190", "6193", "6195", "6196", "6197",
"6208", "6228", "6229", "6232", "6255", "6268", "6269", "6270",
"6275"), class = "factor"), Gender = structure(c(2L, 2L, 1L,
2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L,
1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L,
2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L,
2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L,
1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L,
2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L,
2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L), .Label = c("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, 1L, 4L,
2L, 1L, 2L, 2L, 2L, 4L, 1L, 1L, 3L, 3L, 3L, 4L, 3L, 1L, 4L,
2L, 2L, 3L, 4L, 2L, 2L, 1L, 2L, 3L, 1L, 4L, 4L, 3L, 4L, 4L,
1L, 3L, 4L, 3L, 3L, 1L, 3L, 1L, 1L, 1L, 4L, 2L, 1L, 4L, 3L,
2L, 2L, 3L, 4L, 1L, 2L, 1L, 4L, 2L, 1L, 3L, 1L, 2L, 2L, 4L,
1L, 1L, 4L, 4L, 3L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 4L, 4L, 1L,
2L, 4L, 1L, 2L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 4L, 3L, 2L, 4L,
1L, 1L, 3L, 3L, 3L, 1L, 2L, 4L, 3L, 4L, 1L, 4L, 3L, 2L, 1L,
4L, 4L, 4L, 2L, 4L, 1L, 4L, 2L, 1L, 3L, 1L, 2L, 3L, 3L, 3L,
1L, 1L, 2L, 4L, 4L, 1L, 4L, 1L, 3L, 3L, 4L, 1L, 3L, 4L, 2L,
2L, 1L, 1L, 1L, 1L, 4L, 1L, 4L, 1L, 4L, 2L, 4L, 3L, 3L, 3L,
2L, 2L, 2L, 1L, 4L, 4L, 3L, 3L, 4L, 3L, 1L, 4L, 2L, 2L, 3L,
2L, 3L, 1L, 2L, 3L, 2L, 1L, 3L, 3L, 2L, 4L, 1L, 1L, 2L, 1L,
4L, 4L, 1L, 2L, 1L, 2L, 4L, 2L, 4L, 1L, 4L, 2L, 4L, 3L, 2L
), .Label = c("A", "B","C", "D"), class = "factor"),
value = c(0.326713741, 0.349206239, 0.365954667,
0.313958377, 0.480487555, 0.431199849, 0.446729183, 0.337009728,
0.331222087, 0.386937141, 0.372758657, 0.305083066, 0.504718482,
0.414191663, 0.40949735, 0.271525055, 0.30009532, 0.50117749,
0.387669057, 0.330797315, 0.390679717, 0.452181876, 0.423188657,
0.396808296, 0.388510793, 0.298505336, 0.412985921, 0.327000797,
0.304242313, 0.277513236, 0.394773901, 0.4322685, 0.440891623,
0.439061254, 0.453015536, 0.385896087, 0.452299237, 0.296923041,
0.443324417, 0.420699686, 0.282610774, 0.303566545, 0.535346806,
0.393591255, 0.32561186, 0.309230596, 0.417596817, 0.281766504,
0.445347071, 0.353419632, 0.354420125, 0.429613769, 0.385733992,
0.155136898, 0.485385537, 0.439544022, 0.436584443, 0.458706915,
0.600399196, 0.440390527, 0.362952292, 0.37253055, 0.37306264,
0.371298164, 0.469741255, 0.573943496, 0.283266962, 0.391182601,
0.663566113, 0.517713368, 0.327498972, 0.353969425, 0.443648636,
0.449972481, 0.434426159, 0.305042148, 0.422493547, 0.194572225,
0.331083208, 0.418288261, 0.447215647, 0.429001331, 0.339149892,
0.336879104, 0.471237898, 0.408330619, 0.393405557, 0.486086488,
0.427713692, 0.379242182, 0.40456596, 0.326695889, 0.393235713,
0.452374548, 0.332855165, 0.323469192, 0.396484613, 0.372199923,
0.257353246, 0.405249774, 0.326494843, 0.420468688, 0.335335255,
0.267627925, 0.379383296, 0.338241786, 0.416064918, 0.381003618,
0.284006208, 0.442705005, 0.494199812, 0.464447916, 0.370418996,
0.293953657, 0.34482345, 0.47208631, 0.378798842, 0.407261223,
0.34767586, 0.424341202, 0.434532404, 0.342623293, 0.628901243,
0.381492049, 0.540111601, 0.392371207, 0.459349483, 0.373172134,
0.270272404, 0.413454324, 0.375994682, 0.470298111, 0.340463549,
0.31613645, 0.470312864, 0.410651028, 0.276164204, 0.341546267,
0.402167588, 0.465735435, 0.434102625, 0.328114063, 0.394582212,
0.331681252, 0.387562275, 0.3989245, 0.44939962, 0.29586333,
0.398924828, 0.559520543, 0.392099082, 0.589552164, 0.397368163,
0.375135392, 0.348508835, 0.447002649, 0.407775551, 0.404435992,
0.666776299, 0.265039146, 0.25311482, 0.354386091, 0.44051528,
0.416727781, 0.460624784, 0.455415428, 0.445090771, 0.502343714,
0.393426061, 0.463244319, 0.345586747, 0.291874498, 0.564393103,
0.400276631, 0.41512531, 0.308440536, 0.373545259, 0.272377819,
0.434890926, 0.358394623, 0.414819628, 0.761894882, 0.409700364,
0.403811544, 0.469092041, 0.397044837, 0.312479883, 0.294876397,
0.314414322, 0.428720832, 0.329074681, 0.311423391, 0.444689006,
0.254723012, 0.248710752, 0.270434052, 0.416304022, 0.38875562,
0.396840513, 0.296386898, 0.454476953, 0.474986047, 0.427072734,
0.270839244, 0.426266223, 0.586857438, 0.348018169, 0.386638522,
0.349321723, 0.418692261, 0.295630395, 0.463439822, 0.286190838,
0.336389571, 0.422766507, 0.231764346, 0.358636618, 0.562871873,
0.381515294, 0.28637746)), row.names = c(961L, 1171L, 84L,
608L, 805L, 889L, 818L, 334L, 596L, 177L, 848L, 468L, 987L, 293L,
218L, 318L, 1110L, 969L, 211L, 1064L, 1221L, 1239L, 99L, 70L,
1084L, 448L, 250L, 351L, 586L, 572L, 1026L, 139L, 232L, 632L,
914L, 658L, 1174L, 909L, 280L, 1126L, 368L, 354L, 808L, 1082L,
453L, 612L, 201L, 337L, 644L, 67L, 1177L, 1153L, 694L, 962L,
1040L, 98L, 923L, 1186L, 691L, 650L, 236L, 886L, 307L, 224L,
100L, 1003L, 598L, 230L, 1112L, 779L, 500L, 433L, 861L, 1099L,
103L, 350L, 248L, 1223L, 370L, 260L, 788L, 267L, 454L, 399L,
1069L, 231L, 204L, 1060L, 1208L, 847L, 205L, 578L, 88L, 221L,
518L, 616L, 242L, 1075L, 951L, 165L, 527L, 1089L, 206L, 380L,
431L, 801L, 1025L, 279L, 575L, 624L, 1052L, 1107L, 854L, 344L,
1191L, 86L, 2L, 916L, 659L, 903L, 251L, 436L, 1118L, 796L, 1150L,
77L, 1155L, 693L, 358L, 107L, 1210L, 1048L, 968L, 620L, 1227L,
258L, 1019L, 515L, 4L, 674L, 16L, 405L, 739L, 660L, 629L, 66L,
64L, 365L, 1061L, 1220L, 166L, 1100L, 182L, 759L, 745L, 1131L,
302L, 757L, 938L, 391L, 371L, 119L, 278L, 135L, 117L, 995L, 21L,
1130L, 150L, 1076L, 364L, 1192L, 772L, 844L, 782L, 421L, 561L,
440L, 41L, 1201L, 963L, 706L, 652L, 1129L, 669L, 180L, 1031L,
581L, 390L, 704L, 603L, 625L, 170L, 384L, 899L, 591L, 255L, 830L,
672L, 558L, 983L, 121L, 190L, 451L, 213L, 1068L, 1042L, 234L,
545L, 40L, 543L, 1045L, 464L, 941L, 76L, 959L, 329L, 1179L, 621L,
517L), class = "data.frame")
Which looks like:
# A tibble: 10 x 5
DTI_ID Gender Age ROI value
<fct> <fct> <fct> <fct> <dbl>
1 5927 Male Old A 0.395
2 5634 Male Old C 0.433
3 5547 Female Old B 0.257
4 5979 Male Old C 0.404
5 5660 Male Old A 0.398
6 5876 Female Old D 0.426
7 5518 Male Old A 0.404
8 6001 Female Old D 0.392
9 6042 Male Old A 0.388
10 5821 Male Old A 0.344
ROI is a region of interest within each subject, so all subjects have all 4 ROIs.
I would like to calculate a 2-way ANCOVA 4(ROIs [a/b/c/d] - within) x 2 (Age [young/old] - between) + Gender [covariate] to determine the interaction effects of age and ROI on value, controlling for Gender.
To do that, I calculated:
#2-way ANOVA
res.aov2 <- df %>%
anova_test(value ~ Gender + Age*ROI, within = ROI, wid= DTI_ID)
get_anova_table(res.aov2)
which works fine and outputs:
ANOVA Table (type II tests)
Effect DFn DFd F p p<.05 ges
1 Gender 1 1227 5.196 2.30e-02 * 0.004000
2 Age 1 1227 0.732 3.92e-01 0.000596
3 ROI 3 1227 228.933 6.13e-118 * 0.359000
4 Age:ROI 3 1227 22.258 4.90e-14 * 0.052000
I then want to run a multiple comparisons to generate p values that I can graph over boxplots for visualization of the analyses.
I am using emmeans_test:
# Pairwise comparisons
pwc2 <- df %>%
group_by(ROI) %>%
emmeans_test(value ~ Age, covariate = Gender,
p.adjust.method = "bonferroni")
but receive the error:
Error in contrast.emmGrid(res.emmeans, by = grouping.vars, method = method, : Nonconforming number of contrast coefficients
I cannot figure out why, as the pairwise comparison works fine when I remove the covariate. Does it have to do with a categorical variable being used as a covariate? I am stuck and want to make sure I am reporting the appropriate p-values in my chart.
Adding Gender to group_by as well, allowed the code to run properly.

Is it possible to table() two columns in a way that display the sum horizontally and vertically?

I have
> table(y$treatment,y$WHO)
1 2 3 4
SSTR 43 34 20 27
SSA 23 28 25 0
Control 0 0 0 0
I am looking for a way to display the sum of each row and column generated by table(). Thus, the expected output is:
1 2 3 4
SSTR 43 34 20 27 124
SSA 23 28 25 0 76
Control 0 0 0 0 0
66 62 45 27
I am particularly interested in dplyr-solutions.
My data
y <- structure(list(treatment = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L), .Label = c("SSTR", "SSA", "Control"), class = "factor"),
WHO = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L,
2L, 3L, 3L, 3L, 2L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 4L,
4L, 1L, 4L, 1L, 2L, 1L, 4L, 1L, 1L, 1L, 1L, 2L, 4L, 1L, 2L,
1L, 4L, 1L, 4L, 4L, 4L, 4L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 4L,
4L, 2L, 1L, 2L, 2L, 4L, 4L, 4L, 2L, 4L, 1L, 4L, 4L, 2L, 4L,
4L, 3L, 4L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 3L, 2L, 2L,
2L, 3L, 2L, 3L, 1L, 2L, 3L, 3L, 3L, 1L, 2L, 1L, 2L, 3L, 3L,
1L, 3L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L,
2L, 2L, 3L, 3L, 1L, 3L, 1L, 3L, 3L, 2L, 3L, 1L, 1L, 2L, 1L,
1L, 3L, 2L, 1L, 1L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
2L, 2L, 2L, 2L, 3L, 2L, 3L, 2L, 1L, 2L, 3L, 2L, 2L, 2L, 1L,
3L, 3L, 3L, 2L, 3L, 2L, 3L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L,
2L, 1L, 1L, 3L, 2L, 3L, 1L, 1L, 1L, 1L), .Label = c("1",
"2", "3", "4"), class = "factor")), na.action = structure(c(`152` = 152L,
`193` = 193L), class = "omit"), row.names = c(1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L,
32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L,
45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L,
58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L,
71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L,
84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L, 95L, 96L,
97L, 98L, 99L, 100L, 101L, 102L, 103L, 104L, 105L, 106L, 107L,
108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L, 116L, 117L, 118L,
119L, 120L, 121L, 122L, 123L, 124L, 125L, 126L, 127L, 128L, 129L,
130L, 131L, 132L, 133L, 134L, 135L, 136L, 137L, 138L, 139L, 140L,
141L, 142L, 143L, 144L, 145L, 146L, 147L, 148L, 149L, 150L, 151L,
153L, 154L, 155L, 156L, 157L, 158L, 159L, 160L, 161L, 162L, 163L,
164L, 165L, 166L, 167L, 168L, 169L, 170L, 171L, 172L, 173L, 174L,
175L, 176L, 177L, 178L, 179L, 180L, 181L, 182L, 183L, 184L, 185L,
186L, 187L, 188L, 189L, 190L, 191L, 192L, 194L, 195L, 196L, 197L,
198L, 199L, 200L, 201L, 202L), class = "data.frame")
A possible solution to consider:
x<-table(y$treatment,y$WHO)
rowcoltotals <- addmargins(x, FUN = list(Total = sum), quiet = TRUE)
output:
1 2 3 4 Total
SSTR 43 34 20 27 124
SSA 23 28 25 0 76
Control 0 0 0 0 0
Total 66 62 45 27 200

Mixed Design ANOVA-Error: object of type 'closure' is not subsettable

I'am running a 2X3X3 mixed ANOVA design. Ive been trying to get the student-newman Kuels function to work in R, but I keep getting the error: object of type 'closure' is not subsettable. Any help would be greatly appreciated!
library(ez)
library(ggplot2)
library(nlme)
library(pastecs)
library(reshape)
library(WRS)
library(GAD)
library(multcomp)
library(psych)
library(lmerTest)
CPPData <- read.table(file = "CPPdatar.csv", header=TRUE, sep = ",")
str(CPPData)
CPPData$Test<-as.factor(CPPData$Test)
CPPData$Exposure<-as.factor(CPPData$Exposure)
CPPData$Dose<-as.factor(CPPData$Dose)
CPPData$Subject<-as.factor(CPPData$Subject)
levels(CPPData$Test)<-list("Habit"=1, "Test of Conditioning"=2)
levels(CPPData$Exposure)<-list("0% HFCS CONT"=1, "50% HFCS CONT"=2, "50% HFCS INT"=3)
levels(CPPData$Dose)<-list("0 OXY"=1, "0.16 OXY"=2, "2.5 OXY"=3)
str(CPPData)
options(contrasts=c("contr.helmert", "contr.poly"))
aov1<-aov(Time~Exposure*Dose*Test+ Error(Subject/(Test)), data=CPPData)
summary(aov1)
aov2 = lme(Time~Exposure + Dose + Test,
random = ~1|Subject,
data = CPPData,
method = "ML")
summary(aov2)
#SNK Test
Test2<-snk.test(lm(Time~Exposure*Test*Dose, data = CPPData))
Test1<-snk.test(lm, term = Exposure*Test*Dose, among = Exposure, within = Test*Dose)
snk.test(aov2, term = "Dose:Exposure:Test", among = "Test", within = "Dose")
Error issues:
SNK Test
Test2<-snk.test(lm(Time~ExposureTestDose, data = CPPData))
Error in estimates(object) :
Design unbalanced! This function can only handle balanced designs.
Test1<-snk.test(lm, term = ExposureTestDose, among = Exposure, within = Test*Dose)
Error: object of type 'closure' is not subsettable
> snk.test(aov2, term = "Dose:Exposure:Test", among = "Test", within = "Dose)
Error in object$model[, 2:(length(object$x) + 1)] :
incorrect number of dimensions
Dput
dput(CPPData)
structure(list(Time = c(476.98, 436.94, 451.79, 514.68, 548.38,
457.96, 489.99, 536.7, 517.02, 566.9, 487.15, 553.89, 527.86,
580.41, 310.14, 508.84, 364.7, 456.79, 616.12, 598.43, 447.45,
570.9, 520.35, 812.48, 756.92, 667.5, 603.77, 547.55, 369.2,
437.6, 296.96, 568.74, 580.91, 526.19, 582.92, 568.23, 576.74,
378.04, 549.38, 548.55, 492.99, 388.72, 581.75, 538.71, 511.51,
895.89, 851.68, 685.85, 741.24, 738.4, 662.5, 485.32, 746.75,
726.89, 638.64, 613.11, 819.65, 475.14, 599.93, 668.34, 486.99,
426.26, 570.74, 482.48, 460.13, 578.08, 541.37, 465.63, 297.13,
543.04, 560.89, 536.04, 419.25, 555.89, 587.75, 530.2, 619.95,
778.61, 602.27, 403.74, 792.63, 815.98, 599.77, 784.28, 543.04,
748.08, 610.94, 700.87, 710.38, 686.02, 735.57, 805.47, 525.02,
524.86, 482.65, 278.28, 547.21, 559.23, 450.95, 579.91, 174.17,
312.64, 683.02, 790.46, 422.92, 664, 435.44, 478.14, 427.43,
308.31, 342.84, 517.02, 541.54, 565.73, 474.81, 522.02, 503.34,
321.99, 498.16, 554.55, 527.69, 776.44, 966.47, 867.2, 846.68,
554.22, 750.75, 435.44, 769.94, 561.39, 544.71, 404.24, 464.8,
482.48, 550.88, 459.79, 552.05, 389.56, 361.86, 814.15, 907.41,
755.92, 729.9, 945.11, 509.34, 529.86, 611.78, 675.84, 452.79,
584.42, 566.4, 420.75, 465.46, 533.87, 579.58, 492.16, 521.86,
565.07, 594.76, 280.95, 563.23, 537.37, 409.07, 439.44, 547.88,
543.88, 496.66, 474.14, 433.43, 418.75, 541.37, 406.74, 477.98,
576.58, 335.5, 405.74, 319.49, 482.15, 501, 409.07, 610.28, 667.17,
560.56, 474.14, 467.3, 561.56, 718.88, 584.25, 689.52, 507.51,
711.04, 520.19, 357.52, 373.04, 504, 360.86, 470.97, 489.49,
397.06, 582.41, 554.05, 481.15, 776.78, 658.66, 721.55, 433.93,
532.03, 625.79, 651.48, 783.78, 729.06, 828.5), Test = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L,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 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L), .Label = c("0 OXY", "0.16 OXY", "2.5 OXY"), class = "factor"),
Subject = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 13L, 14L, 15L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 16L,
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L,
41L, 42L, 43L, 44L, 45L, 46L, 31L, 32L, 33L, 34L, 35L, 36L,
37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L,
49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 47L, 48L, 49L, 50L,
51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L,
63L, 64L, 65L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L,
66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 66L, 67L, 68L,
69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L,
81L, 82L, 83L, 84L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L,
83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L,
95L, 96L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L,
95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L, 104L, 105L,
106L, 97L, 98L, 99L, 100L, 101L, 102L, 103L, 104L, 105L,
106L), .Label = c("1", "2", "3", "4", "5", "6", "7", "8",
"9", "10", "11", "12", "13", "14", "15", "16", "17", "18",
"19", "20", "21", "22", "23", "24", "25", "26", "27", "28",
"29", "30", "31", "32", "33", "34", "35", "36", "37", "38",
"39", "40", "41", "42", "43", "44", "45", "46", "47", "48",
"49", "50", "51", "52", "53", "54", "55", "56", "57", "58",
"59", "60", "61", "62", "63", "64", "65", "66", "67", "68",
"69", "70", "71", "72", "73", "74", "75", "76", "77", "78",
"79", "80", "81", "82", "83", "84", "85", "86", "87", "88",
"89", "90", "91", "92", "93", "94", "95", "96", "97", "98",
"99", "100", "101", "102", "103", "104", "105", "106"), class = "factor")), row.names = c(NA,
-212L), class = "data.frame")
The cause of the error you were asking about can be seen here:
Test1 <- snk.test(lm, term = ExposureTestDose, ....
The letters "lm" are the name of a function and you gave that to a function that was expecting a data object that could be subsetted. The first call did give the snk.test function a data object created by lm, but it was stopped by code that gave a different reason.
As far as that other errors go, it appears you need some statistical advice. The snk.test does not appear to be designed for the type of analysis you are running. You might consider asking for help about design issues on hte CrossValidated.com site. (It's another StackExchange forum that handles statistical questions.)

Assigning 4 different players to a station by round with no repeats

I am building an r-script that uses all the possible combinations of 4 different variables: Rounds, Stations, and Players (Male and Female). Below is my dput version of the dataset:
structure(list(x = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 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 1L, 2L, 3L, 4L, 6L, 1L,
2L, 3L, 4L, 5L, 2L, 3L, 4L, 5L, 6L, 1L, 3L, 4L, 5L, 6L, 1L,
2L, 4L, 5L, 6L, 1L, 2L, 3L, 5L, 6L, 1L, 2L, 3L, 4L, 6L, 1L,
2L, 3L, 4L, 5L, 2L, 3L, 4L, 5L, 6L, 1L, 3L, 4L, 5L, 6L, 1L,
2L, 4L, 5L, 6L, 1L, 2L, 3L, 5L, 6L, 1L, 2L, 3L, 4L, 6L, 1L,
2L, 3L, 4L, 5L), .Label = c("female1", "female2", "female3",
"female4", "female5", "female6"), class = "factor")), .Names = c("x",
"x.x", "x.y", "y"), row.names = c(55L, 109L, 163L, 217L, 271L,
10L, 118L, 172L, 226L, 280L, 19L, 73L, 181L, 235L, 289L, 28L,
82L, 136L, 244L, 298L, 37L, 91L, 145L, 199L, 307L, 46L, 100L,
154L, 208L, 262L, 58L, 112L, 166L, 220L, 274L, 13L, 121L, 175L,
229L, 283L, 22L, 76L, 184L, 238L, 292L, 31L, 85L, 139L, 247L,
301L, 40L, 94L, 148L, 202L, 310L, 49L, 103L, 157L, 211L, 265L,
61L, 115L, 169L, 223L, 277L, 16L, 124L, 178L, 232L, 286L, 25L,
79L, 187L, 241L, 295L, 34L, 88L, 142L, 250L, 304L, 43L, 97L,
151L, 205L, 313L, 52L, 106L, 160L, 214L, 268L, 56L, 110L, 164L,
218L, 272L, 11L, 119L, 173L, 227L, 281L, 20L, 74L, 182L, 236L,
290L, 29L, 83L, 137L, 245L, 299L, 38L, 92L, 146L, 200L, 308L,
47L, 101L, 155L, 209L, 263L, 59L, 113L, 167L, 221L, 275L, 14L,
122L, 176L, 230L, 284L, 23L, 77L, 185L, 239L, 293L, 32L, 86L,
140L, 248L, 302L, 41L, 95L, 149L, 203L, 311L, 50L, 104L, 158L,
212L, 266L, 62L, 116L, 170L, 224L, 278L, 17L, 125L, 179L, 233L,
287L, 26L, 80L, 188L, 242L, 296L, 35L, 89L, 143L, 251L, 305L,
44L, 98L, 152L, 206L, 314L, 53L, 107L, 161L, 215L, 269L, 57L,
111L, 165L, 219L, 273L, 12L, 120L, 174L, 228L, 282L, 21L, 75L,
183L, 237L, 291L, 30L, 84L, 138L, 246L, 300L, 39L, 93L, 147L,
201L, 309L, 48L, 102L, 156L, 210L, 264L, 60L, 114L, 168L, 222L,
276L, 15L, 123L, 177L, 231L, 285L, 24L, 78L, 186L, 240L, 294L,
33L, 87L, 141L, 249L, 303L, 42L, 96L, 150L, 204L, 312L, 51L,
105L, 159L, 213L, 267L, 63L, 117L, 171L, 225L, 279L, 18L, 126L,
180L, 234L, 288L, 27L, 81L, 189L, 243L, 297L, 36L, 90L, 144L,
252L, 306L, 45L, 99L, 153L, 207L, 315L, 54L, 108L, 162L, 216L,
270L), class = "data.frame")`
Below are the first 10 rows:
Round Station Partner1 Partner2
55 round1 station1 male1 female2
109 round1 station1 male1 female3
163 round1 station1 male1 female4
217 round1 station1 male1 female5
271 round1 station1 male1 female6
10 round1 station1 male2 female1
118 round1 station1 male2 female3
172 round1 station1 male2 female4
226 round1 station1 male2 female5
280 round1 station1 male2 female6
In this dataset there are the following:
3 Rounds, 3 Stations, 6 Male, 6 Female
How do I do the following:
1) No person should be in the same station twice
2) No person should be in the same round twice
3) No person should be partnered with the same person twice
4) Each station requires 2 male and 2 female
5) Ideally, each person should be with people in a round that have not been with them in previous rounds.
6) A player with the same number at the end (male1, female1) should never be partnered
So the final output should look something like this for one of the rounds:
Round Station Partner1 Partner2
55 round1 station1 male1 female5
109 round1 station1 male3 female4
163 round1 station2 male2 female3
217 round1 station2 male4 female1
271 round1 station3 male5 female6
10 round1 station3 male6 female2
For round2, all 3 stations should still be present but the male and female partners need to change and should follow the rules described above.
Also, let me know if this is mathematically impossible without adding additional stations.
Any help would be great!
Here's a simple solution. I ignore (5) because it does not seem to be a strict requirement (and I don't think it is possible as a strict rule).
This function takes a valid first round as input, and outputs a good configuration for the second round (and the third, if applied again).
I modified your data to have numbers which are much easier to manipulate than strings. Strictly speaking, only the Partner columns (renamed to Male and Female) need to be numbers for the function to work.
# data
r1 = read.table(text = " Round Station Partner1 Partner2
55 round1 station1 male1 female5
109 round1 station1 male3 female4
163 round1 station2 male2 female3
217 round1 station2 male4 female1
271 round1 station3 male5 female6
10 round1 station3 male6 female2", header = T)
# numericize
r1$Round = as.numeric(substr(r1$Round, 6, 6))
r1$Station = as.numeric(substr(r1$Station, start = 8, stop = 8))
r1$Male = as.numeric(substr(r1$Partner1, 5, 5))
r1$Female = as.numeric(substr(r1$Partner2, 7, 7))
r1[c("Partner1", "Partner2")] = list(NULL)
# function
next_round = function(r) {
r$Male = r$Male[c(3, 4, 5, 6, 1, 2)]
r$Female = r$Female[c(5, 6, 1, 2, 3, 4)]
problems = which(r$Male == r$Female)
# switch male problems
switch_with = ifelse(problems %% 2 == 0, problems - 1, problems + 1)
r$Male[c(rbind(switch_with, problems))] = r$Male[c(rbind(problems, switch_with))]
# increment round
r$Round = r$Round + 1
return(r)
}
# demonstration
r1
# Round Station Male Female
# 55 1 1 1 5
# 109 1 1 3 4
# 163 1 2 4 3
# 217 1 2 2 1
# 271 1 3 5 6
# 10 1 3 6 2
(r2 = next_round(r1))
# Round Station Male Female
# 55 2 1 2 6
# 109 2 1 4 2
# 163 2 2 6 5
# 217 2 2 5 4
# 271 2 3 1 3
# 10 2 3 3 1
(r3 = next_round(r2))
# Round Station Male Female
# 55 3 1 6 3
# 109 3 1 5 1
# 163 3 2 1 6
# 217 3 2 3 2
# 271 3 3 4 5
# 10 3 3 2 4
Essentially what this does is that the males and females stay with the same-sex partners from their starting stations. Males move down one station number each round, females move up one station number each round, and if there is ever a Male = Female collision the males at that station switch to correct it. I was inspired by square dancing, where males and females moving in different directions to rotate partners is common.

Resources