I am trying to make a loop so I can have in a list the results that come out of the loop below.
As you can see, my data is taken out from the 10 electrodes of two individuals and I would like to compare every pair of electrodes and model a generalised linear model out of it, and this is where the nested loop comes handy. I used the instructions in Populating a data frame in R in a loop thread and there goes my code. The problem is, it does not write more than one line, it does not iterate.
dfnew <- as.data.frame(matrix(NA,0,4))
x <- c("elec1", "elec2", "estimate", "pvalue")
colnames(dfnew) <- x
for (i in 1:100){ #total numbers of coincidences between the electrodes from both individuals
for (j in 1:10){ #electrodes from individual 1
for (k in 1:10){ #electrodes from individual 2
A <- subset(dyad_data, elec1 == j & elec2 == k)
B1 <- glm(response ~ dyad , data = A, family=Gamma(link = "inverse"))
dfnew[i,1] <- print(j) #it prints the identifier of electrode from individual 1
dfnew[i,2] <- print(k) #it prints the identifier of electrode from individual 2
dfnew[i,3] <- coef(summary(B1))[2, 3]
dfnew[i,4] <- coef(summary(B1))[2, 4]
}}}
The problem is the outcome seems to override previous line and it does not add a row but it stays in the same line.
This is how my A subset looks like with the electrode 1 from individual 1 and electrode 1 of individual 2, so you can have a look of my type of data:
> head(A)
Elec1 Elec2 Dyad response
187 1 1 1 0.09312585
188 1 1 2 0.09561456
189 1 1 3 0.03530233
2374 1 1 4 0.08787908
2375 1 1 5 0.15917199
2376 1 1 6 0.02174757
I would appreciate any help identifying the mistake in my code.