将嵌套循环的结果添加到 R 中数据框的行中
Adding outcome from a nested loop into rows of a data frame in R
我正在尝试创建一个循环,这样我就可以在列表中列出下面循环的结果。
如你所见,我的数据是从两个人的 10 个电极中取出的,我想比较每一对电极并从中建立一个广义线性模型,这就是嵌套循环的来源便利。我使用了 Populating a data frame in R in a loop 线程中的说明,然后我的代码就出现了。问题是,它不写多于一行,它不迭代。
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]
}}}
问题是结果似乎覆盖了前一行,它没有添加一行,而是保持在同一行。
这就是我的 A 子集与个体 1 的电极 1 和个体 2 的电极 1 的样子,因此您可以查看我的数据类型:
> 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
如能帮助我识别代码中的错误,我将不胜感激。
您可以使用 bind_rows
将行附加到数据框的底部,或者 rbind
使用矩阵:
library(dplyr)
df <-
tibble::tribble(
~Elec1, ~Elec2, ~Dyad, ~response,
1, 1, 1, 0.09312585,
1, 1, 2, 0.09561456,
1, 1, 3, 0.03530233,
1, 1, 4, 0.08787908,
1, 1, 5, 0.15917199,
1, 1, 6, 0.02174757,
1, 2, 1, 0.2,
1, 2, 2, 0.3,
1, 2, 3, 0.23,
1, 2, 4, 0.43,
1, 2, 5, 0.44,
1, 2, 6, 0.55
)
summary_df <-
tribble(~Elec1, ~Elec2, ~estimate, ~pvalue)
distinct_pairs <-
df %>%
select(Elec1, Elec2) %>%
distinct()
for (i in 1:nrow(distinct_pairs)) {
model_coefs <-
df %>%
inner_join(distinct_pairs[i, ], by = c("Elec1", "Elec2")) %>%
glm(data = ., formula = response ~ Dyad, family = Gamma(link = "inverse")) %>%
summary() %>%
coef()
summary_df <-
summary_df %>%
bind_rows(
c(distinct_pairs[i, ],
estimate = model_coefs[2, 3],
pvalue = model_coefs[2, 4])
)
}
我相信有更简洁的方法可以解决这个问题,但这应该会给您一些想法。
我正在尝试创建一个循环,这样我就可以在列表中列出下面循环的结果。
如你所见,我的数据是从两个人的 10 个电极中取出的,我想比较每一对电极并从中建立一个广义线性模型,这就是嵌套循环的来源便利。我使用了 Populating a data frame in R in a loop 线程中的说明,然后我的代码就出现了。问题是,它不写多于一行,它不迭代。
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]
}}}
问题是结果似乎覆盖了前一行,它没有添加一行,而是保持在同一行。
这就是我的 A 子集与个体 1 的电极 1 和个体 2 的电极 1 的样子,因此您可以查看我的数据类型:
> 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
如能帮助我识别代码中的错误,我将不胜感激。
您可以使用 bind_rows
将行附加到数据框的底部,或者 rbind
使用矩阵:
library(dplyr)
df <-
tibble::tribble(
~Elec1, ~Elec2, ~Dyad, ~response,
1, 1, 1, 0.09312585,
1, 1, 2, 0.09561456,
1, 1, 3, 0.03530233,
1, 1, 4, 0.08787908,
1, 1, 5, 0.15917199,
1, 1, 6, 0.02174757,
1, 2, 1, 0.2,
1, 2, 2, 0.3,
1, 2, 3, 0.23,
1, 2, 4, 0.43,
1, 2, 5, 0.44,
1, 2, 6, 0.55
)
summary_df <-
tribble(~Elec1, ~Elec2, ~estimate, ~pvalue)
distinct_pairs <-
df %>%
select(Elec1, Elec2) %>%
distinct()
for (i in 1:nrow(distinct_pairs)) {
model_coefs <-
df %>%
inner_join(distinct_pairs[i, ], by = c("Elec1", "Elec2")) %>%
glm(data = ., formula = response ~ Dyad, family = Gamma(link = "inverse")) %>%
summary() %>%
coef()
summary_df <-
summary_df %>%
bind_rows(
c(distinct_pairs[i, ],
estimate = model_coefs[2, 3],
pvalue = model_coefs[2, 4])
)
}
我相信有更简洁的方法可以解决这个问题,但这应该会给您一些想法。