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

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

Cannot use bind_rows to merge output from list created by for-loop

问题

以下是代码部分的翻译:

  1. 让我们假设我有如下指定的数据框`a`
  2. > head(a)
  3. Ki67 simpson WHO event event.tid
  4. 1 25 1 WHO1 1 17
  5. 2 46 3 WHO3 2 48
  6. 3 99 2 WHO2 1 110
  7. 4 57 4 WHO1 2 148
  8. 5 66 1 WHO3 1 3
  9. 6 75 3 WHO2 2 173
  10. 我需要总结循环任务的输出,如下所示。但是,执行时遇到错误。我需要将存储的输出与`y$Ki67`的信息组合起来。
  11. **循环部分**
  12. ```R
  13. library(riskRegression)
  14. library(tidyverse)
  15. df_list <- list()
  16. for(i in c("WHO1", "WHO2", "WHO3")){
  17. for(j in c("1", "2", "3", "4")){
  18. y <- filter(a, WHO == i, simpson == j) %>% droplevels()
  19. x <- Score(list("Ki67" = y$Ki67),
  20. formula = Hist(event.tid, event) ~ 1,
  21. data = y,
  22. metrics = c("auc"),
  23. times = c(36, 60, 120),
  24. plots = "ROC")
  25. df_list[[length(df_list) + 1]] <- list(
  26. WHO = i,
  27. simpson = j,
  28. times = x$ROC$plotframe$times,
  29. tpr = x$ROC$plotframe$TPR,
  30. fpr = x$ROC$plotframe$FPR,
  31. ki67 = sort(unique(y$Ki67))
  32. )
  33. }
  34. }

现在我想要绑定行。通常情况下bind_rows工作得很好,但这次我收到以下错误:

  1. bind_rows(df_list)
  2. > Error in vctrs::data_frame(): 无法将`WHO`(大小为300)循环使用以匹配`ki67`(大小为99)。运行`rlang::last_trace()`以查看错误发生的位置。

如何解决这个问题?我尝试过类似以下的解决方法:

  1. ...
  2. ki67 = rep(sort(unique(y$Ki67)), length(i))
  3. ...

但那并没有解决问题。我希望尽量使用tidyverse解决这个问题。

数据

  1. a <- data.frame(
  2. Ki67 = sample(1:100, size = 6000, replace = TRUE),
  3. simpson = as.factor(sample(1:4)),
  4. WHO = sample(c("WHO1", "WHO2", "WHO3")),
  5. event = sample(1:2),
  6. event.tid = sample(1:200)
  7. )
英文:

Let's say I have dataframe a as specified below.

  1. &gt; head(a)
  2. Ki67 simpson WHO event event.tid
  3. 1 25 1 WHO1 1 17
  4. 2 46 3 WHO3 2 48
  5. 3 99 2 WHO2 1 110
  6. 4 57 4 WHO1 2 148
  7. 5 66 1 WHO3 1 3
  8. 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

  1. library(riskRegression)
  2. library(tidyverse)
  3. df_list &lt;- list()
  4. for(i in c(&quot;WHO1&quot;, &quot;WHO2&quot;, &quot;WHO3&quot;)){
  5. for(j in c(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;)){
  6. y &lt;- filter(a, WHO == i, simpson == j) %&gt;% droplevels()
  7. x &lt;- Score(list(&quot;Ki67&quot; = y$Ki67),
  8. formula = Hist(event.tid, event) ~ 1,
  9. data = y,
  10. metrics = c(&quot;auc&quot;),
  11. times = c(36, 60, 120),
  12. plots = &quot;ROC&quot;)
  13. df_list[[length(df_list) + 1]] &lt;- list(
  14. WHO = i,
  15. simpson = j,
  16. times = x$ROC$plotframe$times,
  17. tpr = x$ROC$plotframe$TPR,
  18. fpr = x$ROC$plotframe$FPR,
  19. ki67 = sort(unique(y$Ki67))
  20. )
  21. }
  22. }

Now I want to bind the rows. Normally bind_rows works perfectly, but this time I get this error:

  1. 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:

  1. ...
  2. ki67 = rep(sort(unique(y$Ki67)), length(i))
  3. ...

But that did not fix the problem. I seek preferably solutions in tidyverse

Data

  1. a &lt;- data.frame(
  2. Ki67 = sample(1:100, size = 6000, replace = TRUE),
  3. simpson = as.factor(sample(1:4)),
  4. WHO = sample(c(&quot;WHO1&quot;, &quot;WHO2&quot;, &quot;WHO3&quot;)),
  5. event = sample(1:2),
  6. event.tid = sample(1:200)
  7. )

答案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:

  1. 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;)
  2. quux
  3. # Ki67 simpson WHO event event.tid
  4. # 1 25 1 WHO1 1 17
  5. # 2 46 3 WHO3 2 48
  6. # 3 99 2 WHO2 1 110
  7. # 4 57 4 WHO1 2 148
  8. # 5 66 1 WHO3 1 3
  9. # 6 75 3 WHO2 2 173
  10. str(quux)
  11. # &#39;data.frame&#39;: 6 obs. of 5 variables:
  12. # $ Ki67 :List of 6
  13. # ..$ : int 25
  14. # ..$ : int 46
  15. # ..$ : int 99
  16. # ..$ : int 57
  17. # ..$ : int 66
  18. # ..$ : int 75
  19. # $ simpson : int 1 3 2 4 1 3
  20. # $ WHO : chr &quot;WHO1&quot; &quot;WHO3&quot; &quot;WHO2&quot; &quot;WHO1&quot; ...
  21. # $ event : int 1 2 1 2 1 2
  22. # $ 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.

  1. library(dplyr)
  2. bind_rows(list(quux,
  3. list(Ki67=list(9:12), simpson=9, WHO=&quot;QUUX&quot;, event=9, event.tid=9)
  4. # ^^^^ &lt;--- you need to add this to your code
  5. ))
  6. # Ki67 simpson WHO event event.tid
  7. # 1 25 1 WHO1 1 17
  8. # 2 46 3 WHO3 2 48
  9. # 3 99 2 WHO2 1 110
  10. # 4 57 4 WHO1 2 148
  11. # 5 66 1 WHO3 1 3
  12. # 6 75 3 WHO2 2 173
  13. # ...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:

  1. 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;)
  2. quux
  3. # Ki67 simpson WHO event event.tid
  4. # 1 25 1 WHO1 1 17
  5. # 2 46 3 WHO3 2 48
  6. # 3 99 2 WHO2 1 110
  7. # 4 57 4 WHO1 2 148
  8. # 5 66 1 WHO3 1 3
  9. # 6 75 3 WHO2 2 173
  10. str(quux)
  11. # &#39;data.frame&#39;: 6 obs. of 5 variables:
  12. # $ Ki67 :List of 6
  13. # ..$ : int 25
  14. # ..$ : int 46
  15. # ..$ : int 99
  16. # ..$ : int 57
  17. # ..$ : int 66
  18. # ..$ : int 75
  19. # $ simpson : int 1 3 2 4 1 3
  20. # $ WHO : chr &quot;WHO1&quot; &quot;WHO3&quot; &quot;WHO2&quot; &quot;WHO1&quot; ...
  21. # $ event : int 1 2 1 2 1 2
  22. # $ 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.

  1. library(dplyr)
  2. bind_rows(list(quux,
  3. list(Ki67=list(9:12), simpson=9, WHO=&quot;QUUX&quot;, event=9, event.tid=9)
  4. # ^^^^ &lt;--- you need to add this to your code
  5. ))
  6. # Ki67 simpson WHO event event.tid
  7. # 1 25 1 WHO1 1 17
  8. # 2 46 3 WHO3 2 48
  9. # 3 99 2 WHO2 1 110
  10. # 4 57 4 WHO1 2 148
  11. # 5 66 1 WHO3 1 3
  12. # 6 75 3 WHO2 2 173
  13. # ...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:

确定