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

问题描述 投票:1回答:2

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

我想基于Species合并行(也就是说,如果它们是相同的鱼类),如果它们同时被Bottom fishingTrolling方法捕获,则会导致两行折叠成一行,改变Method值设为Both

例如Caranx ignobilis将具有新的MethodBothBaitReleasedKept列也应在同一行上具有值。

          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)))

使用不同的捕鱼方法捕获鱼。我想根据种类(如果它们是相同的鱼类)合并行,如果它们被“底钓”和“拖钓”同时捕获...

r dplyr conditional-statements mutate case-when
2个回答
1
投票

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


0
投票

这应该使您入门。您可以将其他列添加到摘要功能。

© www.soinside.com 2019 - 2024. All rights reserved.