gpt4 book ai didi

R dplyr 用第一个非 "missing"值替换 -"missing"列数据

转载 作者:行者123 更新时间:2023-12-04 02:25:25 31 4
gpt4 key购买 nike

要在标题(或谷歌)中简洁地描述这是一个棘手的问题。我有一个分类表,其中某些列可能会根据置信度列为“已删除”。我想用“未识别”替换任何显示“已删除”的列,后跟第一列中未识别的值以行方式说“掉落”。因此,输入将如下所示:

#> # A tibble: 21 x 4
#> domain class order species
#> <chr> <chr> <chr> <chr>
#> 1 Eukaryota dropped dropped dropped
#> 2 Eukaryota dropped dropped dropped
#> 3 Eukaryota dropped dropped dropped
#> 4 Eukaryota dropped dropped dropped
#> 5 Eukaryota dropped dropped dropped
#> 6 Eukaryota dropped dropped dropped
#> 7 Eukaryota Hexanauplia Calanoida dropped
#> 8 Eukaryota dropped dropped dropped
#> 9 Eukaryota Dinophyceae Syndiniales dropped
#> 10 Animals Polychaeta Terebellida dropped
#> 11 Eukaryota Acantharia Chaunacanthida dropped
#> 12 Eukaryota dropped dropped dropped
#> 13 Animals Ascidiacea Stolidobranchia dropped
#> 14 Eukaryota Haptophyta dropped dropped
#> 15 Eukaryota dropped dropped dropped
#> 16 Eukaryota dropped dropped dropped
#> 17 Eukaryota dropped dropped dropped
#> 18 Animals Ascidiacea Stolidobranchia dropped
#> 19 Eukaryota dropped dropped dropped
#> 20 Eukaryota dropped dropped dropped

输出应该是这样的:

#> # A tibble: 21 x 4
#> domain class order species
#> <chr> <chr> <chr> <chr>
#> 1 Eukaryota Unidentified Eukary… Unidentified Eukary… Unidentified Eukaryota
#> 2 Eukaryota Unidentified Eukary… Unidentified Eukary… Unidentified Eukaryota
#> 3 Eukaryota Unidentified Eukary… Unidentified Eukary… Unidentified Eukaryota
#> 4 Eukaryota Unidentified Eukary… Unidentified Eukary… Unidentified Eukaryota
#> 5 Eukaryota Unidentified Eukary… Unidentified Eukary… Unidentified Eukaryota
#> 6 Eukaryota Unidentified Eukary… Unidentified Eukary… Unidentified Eukaryota
#> 7 Eukaryota Hexanauplia Calanoida Unidentified Calanoida
#> 8 Eukaryota Unidentified Eukary… Unidentified Eukary… Unidentified Eukaryota
#> 9 Eukaryota Dinophyceae Syndiniales Unidentified Syndiniales
#> 10 Animals Polychaeta Terebellida Unidentified Terebellida
#> 11 Eukaryota Acantharia Chaunacanthida Unidentified Chaunacanth…
#> 12 Eukaryota Unidentified Eukary… Unidentified Eukary… Unidentified Eukaryota
#> 13 Animals Ascidiacea Stolidobranchia Unidentified Stolidobran…
#> 14 Eukaryota Haptophyta Unidentified Haptop… Unidentified Haptophyta
#> 15 Eukaryota Unidentified Eukary… Unidentified Eukary… Unidentified Eukaryota
#> 16 Eukaryota Unidentified Eukary… Unidentified Eukary… Unidentified Eukaryota
#> 17 Eukaryota Unidentified Eukary… Unidentified Eukary… Unidentified Eukaryota
#> 18 Animals Ascidiacea Stolidobranchia Unidentified Stolidobran…
#> 19 Eukaryota Unidentified Eukary… Unidentified Eukary… Unidentified Eukaryota
#> 20 Eukaryota Unidentified Eukary… Unidentified Eukary… Unidentified Eukaryota

我使用 purrr::pmap_dfr 想出了一个很好的解决方案,但我很想知道是否有更“纯粹”的 dplyr 方法来做到这一点?我的方法中的一个缺陷是它不适用于第一个非“丢弃”列位于一个或多个“丢弃”列之后的列(请参见下面输出中的第 21 行)。这是我目前的解决方案:

library(tidyverse)
otu_table <- structure(list(domain = c("Eukaryota", "Eukaryota", "Eukaryota",
"Eukaryota", "Eukaryota", "Eukaryota", "Eukaryota", "Eukaryota",
"Eukaryota", "Animals", "Eukaryota", "Eukaryota", "Animals",
"Eukaryota", "Eukaryota", "Eukaryota", "Eukaryota", "Animals",
"Eukaryota", "Eukaryota", "dropped"), class = c("dropped", "dropped",
"dropped", "dropped", "dropped", "dropped", "Hexanauplia", "dropped",
"Dinophyceae", "Polychaeta", "Acantharia", "dropped", "Ascidiacea",
"Haptophyta", "dropped", "dropped", "dropped", "Ascidiacea",
"dropped", "dropped", "not dropped"), order = c("dropped", "dropped",
"dropped", "dropped", "dropped", "dropped", "Calanoida", "dropped",
"Syndiniales", "Terebellida", "Chaunacanthida", "dropped", "Stolidobranchia",
"dropped", "dropped", "dropped", "dropped", "Stolidobranchia",
"dropped", "dropped", "dropped"), species = c("dropped", "dropped",
"dropped", "dropped", "dropped", "dropped", "dropped", "dropped",
"dropped", "dropped", "dropped", "dropped", "dropped", "dropped",
"dropped", "dropped", "dropped", "dropped", "dropped", "dropped",
"dropped")), row.names = c(NA, -21L), class = c("tbl_df", "tbl",
"data.frame"))

tax_data <- otu_table %>%
pmap_dfr(~{
items <- list(...)
first_dropped = match("dropped",items)
if (first_dropped > 1) {
dropped_name <- str_c("Unidentified ",items[first_dropped-1])
} else {
dropped_name <- "Unidentified"
}
items[-c(1:first_dropped-1)] <- dropped_name
items
})
print(tax_data,n=30)
#> # A tibble: 21 x 4
#> domain class order species
#> <chr> <chr> <chr> <chr>
#> 1 Eukaryota Unidentified Eukary… Unidentified Eukary… Unidentified Eukaryota
#> 2 Eukaryota Unidentified Eukary… Unidentified Eukary… Unidentified Eukaryota
#> 3 Eukaryota Unidentified Eukary… Unidentified Eukary… Unidentified Eukaryota
#> 4 Eukaryota Unidentified Eukary… Unidentified Eukary… Unidentified Eukaryota
#> 5 Eukaryota Unidentified Eukary… Unidentified Eukary… Unidentified Eukaryota
#> 6 Eukaryota Unidentified Eukary… Unidentified Eukary… Unidentified Eukaryota
#> 7 Eukaryota Hexanauplia Calanoida Unidentified Calanoida
#> 8 Eukaryota Unidentified Eukary… Unidentified Eukary… Unidentified Eukaryota
#> 9 Eukaryota Dinophyceae Syndiniales Unidentified Syndiniales
#> 10 Animals Polychaeta Terebellida Unidentified Terebellida
#> 11 Eukaryota Acantharia Chaunacanthida Unidentified Chaunacanth…
#> 12 Eukaryota Unidentified Eukary… Unidentified Eukary… Unidentified Eukaryota
#> 13 Animals Ascidiacea Stolidobranchia Unidentified Stolidobran…
#> 14 Eukaryota Haptophyta Unidentified Haptop… Unidentified Haptophyta
#> 15 Eukaryota Unidentified Eukary… Unidentified Eukary… Unidentified Eukaryota
#> 16 Eukaryota Unidentified Eukary… Unidentified Eukary… Unidentified Eukaryota
#> 17 Eukaryota Unidentified Eukary… Unidentified Eukary… Unidentified Eukaryota
#> 18 Animals Ascidiacea Stolidobranchia Unidentified Stolidobran…
#> 19 Eukaryota Unidentified Eukary… Unidentified Eukary… Unidentified Eukaryota
#> 20 Eukaryota Unidentified Eukary… Unidentified Eukary… Unidentified Eukaryota
#> 21 dropped not dropped dropped dropped

更新:

下面有一些很好的答案。我接受了投票最多的那个,但事实证明,在通过 microbenchmark 运行所有建议后,purrr 解决方案是最快的,几乎快了一个数量级.

最佳答案

这是另一种方法,结合使用 rowwise()across()

  • 我们正在使用 rowwise,因为它有助于通过 cur_data()
  • 将行用作单个向量
  • across(everything(), ~) 帮助我们一次性改变所有列
  • max.col(cur_data() != 'dropped', ties.method = 'last') 将检索最后一列索引,其中值 != 'dropped'
  • 我们将其列名存储在一个临时变量中,例如 x
  • 最后,我们使用基 R 中的 if()..else 来仅改变值被删除的那些列

希望答案够清楚

library(tidyverse)

otu_table %>% rowwise() %>%
mutate(across(everything(), ~ {x<- names(cur_data())[max.col(cur_data() != 'dropped', ties.method = 'last')];
if (. == 'dropped') paste0('unidentified ', get(x)) else . }))

#> # A tibble: 21 x 4
#> # Rowwise:
#> domain class order species
#> <chr> <chr> <chr> <chr>
#> 1 Eukaryota unidentified Eukaryo~ unidentified Eukaryo~ unidentified Eukaryota
#> 2 Eukaryota unidentified Eukaryo~ unidentified Eukaryo~ unidentified Eukaryota
#> 3 Eukaryota unidentified Eukaryo~ unidentified Eukaryo~ unidentified Eukaryota
#> 4 Eukaryota unidentified Eukaryo~ unidentified Eukaryo~ unidentified Eukaryota
#> 5 Eukaryota unidentified Eukaryo~ unidentified Eukaryo~ unidentified Eukaryota
#> 6 Eukaryota unidentified Eukaryo~ unidentified Eukaryo~ unidentified Eukaryota
#> 7 Eukaryota Hexanauplia Calanoida unidentified Calanoida
#> 8 Eukaryota unidentified Eukaryo~ unidentified Eukaryo~ unidentified Eukaryota
#> 9 Eukaryota Dinophyceae Syndiniales unidentified Syndinial~
#> 10 Animals Polychaeta Terebellida unidentified Terebelli~
#> # ... with 11 more rows

reprex package 创建于 2021-06-19 (v2.0.0)

关于R dplyr 用第一个非 "missing"值替换 -"missing"列数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68042067/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com