英文:
Cannot use bind_rows to merge output from list created by for-loop
问题
以下是代码部分的翻译:
让我们假设我有如下指定的数据框`a`。
> head(a)
Ki67 simpson WHO event event.tid
1 25 1 WHO1 1 17
2 46 3 WHO3 2 48
3 99 2 WHO2 1 110
4 57 4 WHO1 2 148
5 66 1 WHO3 1 3
6 75 3 WHO2 2 173
我需要总结循环任务的输出,如下所示。但是,执行时遇到错误。我需要将存储的输出与`y$Ki67`的信息组合起来。
**循环部分**
```R
library(riskRegression)
library(tidyverse)
df_list <- list()
for(i in c("WHO1", "WHO2", "WHO3")){
for(j in c("1", "2", "3", "4")){
y <- filter(a, WHO == i, simpson == j) %>% droplevels()
x <- Score(list("Ki67" = y$Ki67),
formula = Hist(event.tid, event) ~ 1,
data = y,
metrics = c("auc"),
times = c(36, 60, 120),
plots = "ROC")
df_list[[length(df_list) + 1]] <- list(
WHO = i,
simpson = j,
times = x$ROC$plotframe$times,
tpr = x$ROC$plotframe$TPR,
fpr = x$ROC$plotframe$FPR,
ki67 = sort(unique(y$Ki67))
)
}
}
现在我想要绑定行。通常情况下bind_rows
工作得很好,但这次我收到以下错误:
bind_rows(df_list)
> Error in vctrs::data_frame(): 无法将`WHO`(大小为300)循环使用以匹配`ki67`(大小为99)。运行`rlang::last_trace()`以查看错误发生的位置。
如何解决这个问题?我尝试过类似以下的解决方法:
...
ki67 = rep(sort(unique(y$Ki67)), length(i))
...
但那并没有解决问题。我希望尽量使用tidyverse
解决这个问题。
数据
a <- data.frame(
Ki67 = sample(1:100, size = 6000, replace = TRUE),
simpson = as.factor(sample(1:4)),
WHO = sample(c("WHO1", "WHO2", "WHO3")),
event = sample(1:2),
event.tid = sample(1:200)
)
英文:
Let's say I have dataframe a
as specified below.
> head(a)
Ki67 simpson WHO event event.tid
1 25 1 WHO1 1 17
2 46 3 WHO3 2 48
3 99 2 WHO2 1 110
4 57 4 WHO1 2 148
5 66 1 WHO3 1 3
6 75 3 WHO2 2 173
I need to summarize the output from a repetitive task as indicated in the for-loop below. However, I encounter an error when doing so. I need to combine the stored output with information from the y$Ki67
.
for loop
library(riskRegression)
library(tidyverse)
df_list <- list()
for(i in c("WHO1", "WHO2", "WHO3")){
for(j in c("1", "2", "3", "4")){
y <- filter(a, WHO == i, simpson == j) %>% droplevels()
x <- Score(list("Ki67" = y$Ki67),
formula = Hist(event.tid, event) ~ 1,
data = y,
metrics = c("auc"),
times = c(36, 60, 120),
plots = "ROC")
df_list[[length(df_list) + 1]] <- list(
WHO = i,
simpson = j,
times = x$ROC$plotframe$times,
tpr = x$ROC$plotframe$TPR,
fpr = x$ROC$plotframe$FPR,
ki67 = sort(unique(y$Ki67))
)
}
}
Now I want to bind the rows. Normally bind_rows
works perfectly, but this time I get this error:
bind_rows(df_list)
> Error in vctrs::data_frame()
: ! Can't recycle WHO
(size 300) to
> match ki67
(size 99). Run rlang::last_trace()
to see where the
> error occurred.
How can I fix this issue? I tried something like:
...
ki67 = rep(sort(unique(y$Ki67)), length(i))
...
But that did not fix the problem. I seek preferably solutions in tidyverse
Data
a <- data.frame(
Ki67 = sample(1:100, size = 6000, replace = TRUE),
simpson = as.factor(sample(1:4)),
WHO = sample(c("WHO1", "WHO2", "WHO3")),
event = sample(1:2),
event.tid = sample(1:200)
)
答案1
得分: 0
以下是您要翻译的内容:
"It's hard to know for sure, but I'm going to assume:
- you mean
Ki67
in all places you listki67
; and - the
Ki67
is a list-column.
Sample data:
quux <- structure(list(Ki67 = list(25L, 46L, 99L, 57L, 66L, 75L), simpson = c(1L, 3L, 2L, 4L, 1L, 3L), WHO = c("WHO1", "WHO3", "WHO2", "WHO1", "WHO3", "WHO2"), event = c(1L, 2L, 1L, 2L, 1L, 2L), event.tid = c(17L, 48L, 110L, 148L, 3L, 173L)), row.names = c("1", "2", "3", "4", "5", "6"), class = "data.frame")
quux
# Ki67 simpson WHO event event.tid
# 1 25 1 WHO1 1 17
# 2 46 3 WHO3 2 48
# 3 99 2 WHO2 1 110
# 4 57 4 WHO1 2 148
# 5 66 1 WHO3 1 3
# 6 75 3 WHO2 2 173
str(quux)
# 'data.frame': 6 obs. of 5 variables:
# $ Ki67 :List of 6
# ..$ : int 25
# ..$ : int 46
# ..$ : int 99
# ..$ : int 57
# ..$ : int 66
# ..$ : int 75
# $ simpson : int 1 3 2 4 1 3
# $ WHO : chr "WHO1" "WHO3" "WHO2" "WHO1" ...
# $ event : int 1 2 1 2 1 2
# $ event.tid: int 17 48 110 148 3 173
Notice that quux$Ki67
looks like a regular column but is a nested list.
With this, your appended row should have this column's new value in a list.
library(dplyr)
bind_rows(list(quux,
list(Ki67=list(9:12), simpson=9, WHO="QUUX", event=9, event.tid=9)
# ^^^^ <--- you need to add this to your code
))
# Ki67 simpson WHO event event.tid
# 1 25 1 WHO1 1 17
# 2 46 3 WHO3 2 48
# 3 99 2 WHO2 1 110
# 4 57 4 WHO1 2 148
# 5 66 1 WHO3 1 3
# 6 75 3 WHO2 2 173
# ...7 9, 10, 11, 12 9 QUUX 9 9
The leading ...7
is just a bad default row name. The 9, 10, 11, 12
is not a comma-separated string, this is how R shows multiple elements within a list-column row."
英文:
It's hard to know for sure, but I'm going to assume:
- you mean
Ki67
in all places you listki67
; and - the
Ki67
is a list-column.
Sample data:
quux <- structure(list(Ki67 = list(25L, 46L, 99L, 57L, 66L, 75L), simpson = c(1L, 3L, 2L, 4L, 1L, 3L), WHO = c("WHO1", "WHO3", "WHO2", "WHO1", "WHO3", "WHO2"), event = c(1L, 2L, 1L, 2L, 1L, 2L), event.tid = c(17L, 48L, 110L, 148L, 3L, 173L)), row.names = c("1", "2", "3", "4", "5", "6"), class = "data.frame")
quux
# Ki67 simpson WHO event event.tid
# 1 25 1 WHO1 1 17
# 2 46 3 WHO3 2 48
# 3 99 2 WHO2 1 110
# 4 57 4 WHO1 2 148
# 5 66 1 WHO3 1 3
# 6 75 3 WHO2 2 173
str(quux)
# 'data.frame': 6 obs. of 5 variables:
# $ Ki67 :List of 6
# ..$ : int 25
# ..$ : int 46
# ..$ : int 99
# ..$ : int 57
# ..$ : int 66
# ..$ : int 75
# $ simpson : int 1 3 2 4 1 3
# $ WHO : chr "WHO1" "WHO3" "WHO2" "WHO1" ...
# $ event : int 1 2 1 2 1 2
# $ event.tid: int 17 48 110 148 3 173
Notice that quux$Ki67
looks like a regular column but is a nested list.
With this, your appended row should have this column's new value in a list.
library(dplyr)
bind_rows(list(quux,
list(Ki67=list(9:12), simpson=9, WHO="QUUX", event=9, event.tid=9)
# ^^^^ <--- you need to add this to your code
))
# Ki67 simpson WHO event event.tid
# 1 25 1 WHO1 1 17
# 2 46 3 WHO3 2 48
# 3 99 2 WHO2 1 110
# 4 57 4 WHO1 2 148
# 5 66 1 WHO3 1 3
# 6 75 3 WHO2 2 173
# ...7 9, 10, 11, 12 9 QUUX 9 9
The leading ...7
is just a bad default row name. The 9, 10, 11, 12
is not a comma-separated string, this is how R shows multiple elements within a list-column row.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论