无法使用bind_rows来合并由for循环创建的列表输出。

huangapple go评论81阅读模式
英文:

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.

&gt; 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 &lt;- list()

for(i in c(&quot;WHO1&quot;, &quot;WHO2&quot;, &quot;WHO3&quot;)){
  
  for(j in c(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;)){
    
    y &lt;- filter(a, WHO == i, simpson == j) %&gt;% droplevels()
    
    x &lt;- Score(list(&quot;Ki67&quot; =  y$Ki67),
               formula = Hist(event.tid, event) ~ 1,
               data = y,
               metrics = c(&quot;auc&quot;),
               times = c(36, 60, 120),
               plots = &quot;ROC&quot;)
    
    df_list[[length(df_list) + 1]] &lt;- 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 &lt;- data.frame(
  Ki67 = sample(1:100, size = 6000, replace = TRUE),
  simpson = as.factor(sample(1:4)),
  WHO = sample(c(&quot;WHO1&quot;, &quot;WHO2&quot;, &quot;WHO3&quot;)),
  event = sample(1:2),
  event.tid = sample(1:200)
)

答案1

得分: 0

以下是您要翻译的内容:

"It's hard to know for sure, but I'm going to assume:

  1. you mean Ki67 in all places you list ki67; and
  2. the Ki67 is a list-column.

Sample data:

quux &lt;- structure(list(Ki67 = list(25L, 46L, 99L, 57L, 66L, 75L), simpson = c(1L, 3L, 2L, 4L, 1L, 3L), WHO = c(&quot;WHO1&quot;, &quot;WHO3&quot;, &quot;WHO2&quot;, &quot;WHO1&quot;, &quot;WHO3&quot;, &quot;WHO2&quot;), event = c(1L, 2L, 1L, 2L, 1L, 2L), event.tid = c(17L, 48L, 110L, 148L, 3L, 173L)), row.names = c(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;, &quot;6&quot;), class = &quot;data.frame&quot;)
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)
# &#39;data.frame&#39;:	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  &quot;WHO1&quot; &quot;WHO3&quot; &quot;WHO2&quot; &quot;WHO1&quot; ...
#  $ 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=&quot;QUUX&quot;, event=9, event.tid=9)
  #         ^^^^  &lt;--- 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:

  1. you mean Ki67 in all places you list ki67; and
  2. the Ki67 is a list-column.

Sample data:

quux &lt;- structure(list(Ki67 = list(25L, 46L, 99L, 57L, 66L, 75L), simpson = c(1L, 3L, 2L, 4L, 1L, 3L), WHO = c(&quot;WHO1&quot;, &quot;WHO3&quot;, &quot;WHO2&quot;, &quot;WHO1&quot;, &quot;WHO3&quot;, &quot;WHO2&quot;), event = c(1L, 2L, 1L, 2L, 1L, 2L), event.tid = c(17L, 48L, 110L, 148L, 3L, 173L)), row.names = c(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;, &quot;6&quot;), class = &quot;data.frame&quot;)
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)
# &#39;data.frame&#39;:	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  &quot;WHO1&quot; &quot;WHO3&quot; &quot;WHO2&quot; &quot;WHO1&quot; ...
#  $ 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=&quot;QUUX&quot;, event=9, event.tid=9)
  #         ^^^^  &lt;--- 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.

huangapple
  • 本文由 发表于 2023年6月26日 20:47:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556835.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定