如何有条件地合并具有多个值的两行并在R中进行变异?
人们用不同的捕鱼方法捕鱼 我想根据如何有条件地合并具有多个值的两行并在R中进行变异?,r,dplyr,conditional-statements,mutate,case-when,R,Dplyr,Conditional Statements,Mutate,Case When,人们用不同的捕鱼方法捕鱼 我想根据鱼种(如果它们是相同的鱼种)合并行,如果它们同时被底钓和拖网方法捕获,将导致两行合并为一行,将方法值更改为两者 例如,Caranx ignobilis将有一个新的方法值Both诱饵已发布和保留列也应在同一行上具有值 Species Method Bait Released Kept 4 Caranx ignobilis Both NA
鱼种
(如果它们是相同的鱼种)合并行,如果它们同时被底钓
和拖网
方法捕获,将导致两行合并为一行,将方法
值更改为两者
例如,Caranx ignobilis
将有一个新的方法
值Both
<代码>诱饵已发布
和保留
列也应在同一行上具有值
Species Method Bait Released Kept
4 Caranx ignobilis Both NA 1 1
这看起来很简单,但我已经挠了好几个小时的头,在tidyverse
软件包中玩弄case\u
TIBLE是以前使用group\u by
和pivot\u wide
对数据进行子设置的结果
这就是示例的外观:
这应该让你开始。您可以将其他列添加到summary函数中
library(tidyverse)
fish_catch %>% select(-Bait, -Released, -Kept) %>%
group_by(Species) %>%
summarize(Method = paste0(Method, collapse = "")) %>%
mutate(Method = fct_recode(Method, "both" = "TrollingBottom fishing"))
# A tibble: 9 x 2
Species Method
<chr> <fct>
1 Aethaloperca rogaa Bottom fishing
2 Aprion virescens Bottom fishing
3 Balistidae spp. Bottom fishing
4 Caranx ignobilis both
5 Epinephelus fasciatus Bottom fishing
6 Epinephelus multinotatus Bottom fishing
7 Other species Bottom fishing
8 Thunnus albacares Trolling
9 Variola louti Bottom fishing
库(tidyverse)
鱼类捕获量%>%选择(-Bait,-释放,-保留)%%>%
组别(种类)%>%
汇总(方法=paste0(方法,collapse=”“)%>%
变异(方法=fct_重新编码(方法,“两者”=“拖网捕鱼”))
#一个tibble:9x2
种法
1罗加Aethaloperca海底捕捞
2.绿杏底钓
3.鲈鱼科海底捕捞
两种都有4只金枪鱼
5扁带石斑鱼底钓
6多点石斑鱼底钓
7其他种类的底栖捕捞
8长鳍金枪鱼
9 Variola louti海底打捞
以下是一种使用tidyverse
的方法。如果底钓和拖网都包含在该物种的方法中,您可以按(物种)分组,并将方法设置为“两者”。然后,您可以根据
物种和方法对_进行分组,并使用填充
以已知值替换NA
。最后,使用slice
为每个物种/方法保留一行。这假设每个物种/方法都有一行-如果不是这样,请告诉我
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
#一个tible:9 x 5
#组:物种,方法[9]
种法投放毒饵
1罗加Aethaloperca底鱼NA 2
2杏绿底鱼NA NA 1
3魟魟科底鱼NA 1
4条银鲫和银鲫1
5扁带石斑鱼底鱼NA 3 NA
6多点石斑鱼底鱼NA 5
7种其他底鱼NA 1 NA
8长鳍金枪鱼1
9 Variola louti底部打捞NA 1
其他列应使用什么值?你能说明一下关于卡兰克斯·伊格诺比利斯
的一行应该是什么样子的吗?卡兰克斯·伊格诺比利斯是通过“拖网”和“保留”(n=1)捕获的,第二条卡兰克斯·伊格诺比利斯是通过“底钓”和“释放”(n=1)捕获的。列应读为“诱饵”NA“释放”1“保留”1。编辑过的问题@nicolareal nice非常有效,谢谢@ben yes需要切片(1)
。当执行
而不是执行ifelse时,是否可以执行case\u?很高兴听到这个消息。关于案例,当有兴趣了解更多细节和细节时。但是等价物可能是这样的:mutate(Method=case\u when(all(c)(“Bottom fishing”,“Trolling”)%在%Method中)~“两者”,TRUE~as.character(Method))
@JL\u sey no,ofc你应该使用求和函数或任何你想做的数学运算
library(tidyverse)
fish_catch %>% select(-Bait, -Released, -Kept) %>%
group_by(Species) %>%
summarize(Method = paste0(Method, collapse = "")) %>%
mutate(Method = fct_recode(Method, "both" = "TrollingBottom fishing"))
# A tibble: 9 x 2
Species Method
<chr> <fct>
1 Aethaloperca rogaa Bottom fishing
2 Aprion virescens Bottom fishing
3 Balistidae spp. Bottom fishing
4 Caranx ignobilis both
5 Epinephelus fasciatus Bottom fishing
6 Epinephelus multinotatus Bottom fishing
7 Other species Bottom fishing
8 Thunnus albacares Trolling
9 Variola louti Bottom fishing
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