如果列不存在,则在嵌套的tibble中创建列。

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

creating columns in nested tibble if column does not exist

问题

  1. transmute(df, a_s = coalesce(a$Species, NA),
  2. b_s = coalesce(b$Species, NA),
  3. c_s = coalesce(c$Species, NA))
英文:

I am trying to extract data from a nested tibble. Within the outer tibble, not all tibbles may exist or be complete. In case of an non-existing column I would like to return 0.

  1. df <- tibble(a = tibble(iris),
  2. b = tibble(iris[1:2]),
  3. c = NULL)

now I'd like to extract the column 'species' from each nested tibble, where the generated column is filled with NA if no data are available. So that the result equals:

  1. tibble(a_s = iris$Species,
  2. b_s = NA,
  3. c_s = NA)

Is there any way I could achieve this?

I naively tried:

  1. transmute(df, a_s = a$species,
  2. b_s = b$species,
  3. c_s = c$species)

which of course only works for a_s,
generates a warning for b_s and throws an error for c_s.

I have tried creating a helper function to evaluate the existence of each column, but this didn't work for nested dataframes. Any ideas on how to solve this?

UPDATE: for clarity, I always want to generate the output as specified, while tibble c may or may not be there.

答案1

得分: 1

使用 greplifelse 内部检查 Species 并使用 do.call 获取最终的 tibble

  1. 库(dplyr)
  2. do.call(tibble, sapply(c("a", "b", "c"), function(x)
  3. ifelse(any(grepl("Species", names(df[[x]]))),
  4. df[[x]]["Species"],
  5. NA_character_))) %>%
  6. rename_with(~ paste0(.x, "_s"))
  7. # A tibble: 150 × 3
  8. a_s b_s c_s
  9. <fct> <chr> <chr>
  10. 1 setosa NA NA
  11. 2 setosa NA NA
  12. 3 setosa NA NA
  13. 4 setosa NA NA
  14. 5 setosa NA NA
  15. 6 setosa NA NA
  16. 7 setosa NA NA
  17. 8 setosa NA NA
  18. 9 setosa NA NA
  19. 10 setosa NA NA
  20. # … with 140 more rows
  21. # ℹ Use `print(n = ...)` to see more rows
英文:

Using grepl within ifelse to check for Species and do.call to get the final tibble.

  1. library(dplyr)
  2. do.call(tibble, sapply(c(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;), function(x)
  3. ifelse(any(grepl(&quot;Species&quot;, names(df[[x]]))),
  4. df[[x]][&quot;Species&quot;],
  5. NA_character_))) %&gt;%
  6. rename_with(~ paste0(.x, &quot;_s&quot;))
  7. # A tibble: 150 &#215; 3
  8. a_s b_s c_s
  9. &lt;fct&gt; &lt;chr&gt; &lt;chr&gt;
  10. 1 setosa NA NA
  11. 2 setosa NA NA
  12. 3 setosa NA NA
  13. 4 setosa NA NA
  14. 5 setosa NA NA
  15. 6 setosa NA NA
  16. 7 setosa NA NA
  17. 8 setosa NA NA
  18. 9 setosa NA NA
  19. 10 setosa NA NA
  20. # … with 140 more rows
  21. # ℹ Use `print(n = ...)` to see more rows

huangapple
  • 本文由 发表于 2023年5月11日 01:40:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221232.html
匿名

发表评论

匿名网友

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

确定