r - 如何有条件地将具有多个值的两行合并在一起并在 R 中进行变异?

标签 r conditional-statements dplyr case-when

使用不同的捕鱼方法捕获了鱼。

如果海底捕鱼拖钓同时捕获了它们,我想根据种类合并行(即它们是相同的鱼种) 方法将导致两行折叠成一行,将 Method 值更改为 Both

例如,Caranx ignobilis 的新 Method 值为 BothBait ReleasedKept 列也应该在同一行上有值。

          Species                  Method       Bait     Released  Kept
        4 Caranx ignobilis         Both         NA       1         1

这看起来很简单,但我已经挠头好几个小时了,并把 case_when 作为 tidyverse 包的一部分。

小标题是之前使用group_bypivot_wider对数据进行子设置的结果。

示例如下:

# A tibble: 10 x 5
# Groups:   Species [9]
   Species                  Method          Bait Released  Kept
   <chr>                    <fct>          <int>    <int> <int>
 1 Aethaloperca rogaa       Bottom fishing    NA       NA     2
 2 Aprion virescens         Bottom fishing    NA       NA     1
 3 Balistidae spp.          Bottom fishing    NA       NA     1
 4 Caranx ignobilis         Trolling          NA       NA     1
 5 Caranx ignobilis         Bottom fishing    NA        1    NA
 6 Epinephelus fasciatus    Bottom fishing    NA        3    NA
 7 Epinephelus multinotatus Bottom fishing    NA       NA     5
 8 Other species            Bottom fishing    NA        1    NA
 9 Thunnus albacares        Trolling          NA       NA     1
10 Variola louti            Bottom fishing    NA       NA     1

数据:

fish_catch <- structure(list(Species = c("Aethaloperca rogaa", "Aprion virescens","Balistidae spp.", "Caranx ignobilis", "Caranx ignobilis", "Epinephelus fasciatus","Epinephelus multinotatus", "Other species", "Thunnus albacares","Variola louti"),
              Method = structure(c(1L, 1L, 1L, 2L, 1L, 1L,1L, 1L, 2L, 1L), .Label = c("Bottom fishing", "Trolling"), class = "factor"),Bait = c(NA_integer_, NA_integer_, NA_integer_, NA_integer_,NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,NA_integer_),
              Released = c(NA, NA, NA, NA, 1L, 3L, NA, 1L,NA, NA),
              Kept = c(2L, 1L, 1L, 1L, NA, NA, 5L, NA, 1L, 1L)), class = c("grouped_df","tbl_df", "tbl", "data.frame"), row.names = c(NA, -10L), groups = structure(list(Species = c("Aethaloperca rogaa", "Aprion virescens",
              "Balistidae spp.","Caranx ignobilis", "Epinephelus fasciatus", "Epinephelus multinotatus","Other species", "Thunnus albacares", "Variola louti"), .rows = list(1L, 2L, 3L, 4:5, 6L, 7L, 8L, 9L, 10L)), row.names = c(NA,-9L), class = c("tbl_df", "tbl", "data.frame"), .drop = FALSE)) 

我要走的路线,但后来我意识到它没有包含物种或其他列

    mutate(Method = case_when(Method == "Bottom fishing" & Method == "Trolling" ~ "Both",
                                 Method == "Bottom fishing" ~ "Bottom fishing",
                                 Method == "Trolling" ~ "Trolling", TRUE ~ as.character(MethodCaught)))

最佳答案

这是一种使用 tidyverse 的方法。如果该物种的方法中同时包含底钓和拖钓,您可以group_by(物种)并将方法设置为“两者”。然后,您可以group_by 物种和方法,并使用fillNA 替换为已知值。最后,使用 slice 为每个物种/方法保留一行。这假设您每个物种/方法都有 1 行 - 如果情况并非如此,请告诉我。

library(tidyverse)

fish_catch %>%
  group_by(Species) %>%
  mutate(Method = ifelse(all(c("Bottom fishing", "Trolling") %in% Method), "Both", as.character(Method))) %>%
  group_by(Species, Method) %>%
  fill(c(Bait, Released, Kept), .direction = "updown") %>%
  slice(1)

输出

# A tibble: 9 x 5
# Groups:   Species, Method [9]
  Species                  Method          Bait Released  Kept
  <chr>                    <chr>          <int>    <int> <int>
1 Aethaloperca rogaa       Bottom fishing    NA       NA     2
2 Aprion virescens         Bottom fishing    NA       NA     1
3 Balistidae spp.          Bottom fishing    NA       NA     1
4 Caranx ignobilis         Both              NA        1     1
5 Epinephelus fasciatus    Bottom fishing    NA        3    NA
6 Epinephelus multinotatus Bottom fishing    NA       NA     5
7 Other species            Bottom fishing    NA        1    NA
8 Thunnus albacares        Trolling          NA       NA     1
9 Variola louti            Bottom fishing    NA       NA     1

关于r - 如何有条件地将具有多个值的两行合并在一起并在 R 中进行变异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61501873/

相关文章:

r - 循环以在 Shiny 的 tabsetPanel 中创建选项卡

python - Groupby相邻行 Pandas 的条件总和

r - 如果 R 中另一列中的值是连续的,则追加列的值

r - 按元素组合向量

使用 dplyr 的递归函数

r - 在 Shiny 的应用程序中的 svg 文件中添加缩放重置按钮

r - 基于不同变量的交互式加入r

使用 writeogr() 下载 Shapefile 时出现 R Shiny downloadHandler() 错误

php - 如何检查我是否已连接到数据库?

python - Pandas :如何根据其他列值的条件对列求和?