如何使用 r_bind 和 select 对多个变量进行分组?

发布于 2025-01-10 18:33:10 字数 9991 浏览 0 评论 0原文

我的数据如下所示:

dput(srkw.dat)
structure(list(year = c(1962L, 1976L, 1981L, 1981L, 1982L, 
1987L, 1989L, 1990L, 1992L, 1992L, 1992L, 1994L, 1998L, 2003L, 
2003L, 2003L, 2003L, 2004L, 2004L, 2004L, 2005L, 2005L, 2005L, 
2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2006L, 2006L, 
2006L, 2006L, 2006L, 2006L, 2007L, 2007L, 2007L, 2008L, 2008L, 
2008L, 2009L, 2009L, 2009L, 2010L, 2010L, 2010L, 2011L, 2011L, 
2013L, 2013L, 2013L, 2014L, 2014L, 2014L, 2014L, 2014L, 2015L, 
2015L, 2015L, 2015L, 2015L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2017L, 2018L, 2018L, 2018L, 2019L, 2020L, 2020L, 2020L, 
2020L), whaleid1 = c("Lx", "Lx", "Lx", "Lx", "Lx", "L5", 
"Lx", "Kx", "L5", "Lx", "L21", "L5", "Lx", "Jx", "L5", "L7", 
"Jx", "L21", "L54", "Lx", "Jx", "J27", "J16", "J11", "Jx", 
"L5", "L5", "J30", "L95", "Lx", "Jx", "J35", "K40", "Lx", 
"L12", "Jx", "L106", "L21", "Kx", "L83", "L83", "L57", "J31", 
"J27", "J30", "L55", "L22", "K36", "Jx", "L72", "K21", NA, 
"K16", "J16", "J35", "L72", "J36", "L22", "K22", "K22", "L77", 
"J47", "Jx", "J40", "L119", "J2", "J35", "L103", "L77", "L87", 
"J39", "J22", "J38", "L115", "J31", NA, "J37", "Lx"), whalesex1 = c(NA, 
0L, NA, NA, 0L, 0L, NA, NA, 0L, 0L, 0L, 0L, 1L, NA, 0L, 0L, 
NA, 0L, 0L, NA, NA, 1L, 0L, 0L, NA, 0L, 0L, 1L, 1L, NA, NA, 
0L, 0L, NA, 0L, NA, 1L, 0L, NA, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 
0L, 0L, NA, 0L, 1L, NA, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
1L, NA, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, NA, 
0L, NA), whaleage1 = c(NA, NA, NA, NA, NA, 23L, NA, NA, 28L, 
NA, 42L, 30L, NA, NA, 39L, 42L, NA, 54L, 27L, NA, NA, 14L, 
33L, 33L, NA, 41L, 41L, 10L, 9L, NA, NA, 8L, 43L, NA, 73L, 
NA, 2L, 57L, NA, 18L, 18L, 31L, 14L, 18L, 14L, 33L, 39L, 
7L, NA, 25L, 27L, NA, 28L, 42L, 16L, 28L, 15L, 43L, 28L, 
28L, 28L, 5L, NA, 12L, 4L, 60L, 18L, 13L, 29L, 24L, 15L, 
23L, 15L, 9L, 25L, NA, 19L, NA), whaleid2 = c("Lx", "Lx", 
"Lx", "Lx", "Lx", "L58", "Lx", "Kx", "L58", "Lx", "Lx", "L58", 
NA, "Jx", "L73", "L57", "Jx", "L47", "L100", "Lx", NA, "J31", 
"J26", "J27", "Jx", "L73", "L73", "J31", "L72", "Lx", "Jx", 
"J37", "K16", "Lx", "L41", NA, "L41", "L26", NA, "L110", 
"L110", NA, "J36", "J31", "Kx", NA, "L94", "K42", NA, "Kx", 
"K25", NA, NA, "J26", "J47", "L105", "J41", "L94", NA, "K33", 
"L119", NA, NA, NA, NA, NA, "J47", "L109", "L119", "Jx", 
NA, "J40", NA, NA, "J56", NA, "Jx", "Lx"), whalesex2 = c(NA, 
NA, NA, NA, NA, 1L, NA, NA, 1L, 0L, NA, 1L, NA, NA, 1L, 1L, 
NA, 0L, 1L, NA, NA, 0L, 1L, 1L, NA, 1L, 1L, 0L, 0L, NA, NA, 
0L, 0L, NA, 1L, NA, 1L, 0L, NA, 1L, 1L, NA, 0L, 0L, NA, NA, 
0L, 1L, NA, NA, 1L, NA, NA, 1L, 1L, 1L, 0L, 0L, NA, 1L, 0L, 
NA, NA, NA, NA, NA, 1L, 1L, 0L, NA, NA, 0L, NA, NA, 0L, NA, 
NA, NA), whaleage2 = c(NA, NA, NA, NA, NA, 7L, NA, NA, 12L, 
NA, NA, 14L, NA, NA, 17L, 26L, NA, 30L, 3L, NA, NA, 10L, 
14L, 14L, NA, 19L, 19L, 10L, 19L, NA, NA, 5L, 21L, NA, 29L, 
NA, 30L, 51L, NA, 1L, 1L, NA, 10L, 14L, NA, NA, 15L, 2L, 
NA, NA, 22L, NA, NA, 23L, 4L, 10L, 9L, 19L, NA, 14L, 3L, 
NA, NA, NA, NA, NA, 6L, 9L, 4L, NA, NA, 14L, NA, NA, 1L, 
NA, NA, NA), whaleid3 = c(NA, NA, NA, NA, "Lx", "L73", "Lx", 
"Kx", "L73", "Lx", NA, "L73", NA, "Jx", NA, NA, NA, "Lx", 
NA, NA, NA, "Jx", "J36", "J39", NA, "L67", "L67", NA, NA, 
NA, NA, "J40", "K35", "Lx", "L77", NA, "L57", "L47", NA, 
"L47", "L91", NA, "J39", "J39", NA, NA, "L113", NA, NA, NA, 
"K26", NA, NA, "J36", NA, NA, "J42", "L113", NA, NA, NA, 
NA, NA, NA, NA, NA, "L87", "L123", "L113", "Jx", NA, "J49", 
NA, NA, "J36", NA, NA, "Lx"), whalesex3 = c(NA, NA, NA, NA, 
0L, 1L, NA, NA, 1L, NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, 0L, 1L, NA, 0L, 0L, NA, NA, NA, NA, 0L, 1L, NA, 
0L, NA, 1L, 0L, NA, 0L, 0L, NA, 1L, 1L, NA, NA, 0L, NA, NA, 
NA, 1L, NA, NA, 0L, NA, NA, 0L, 0L, NA, NA, NA, NA, NA, NA, 
NA, NA, 1L, 1L, 0L, 0L, NA, 1L, NA, NA, 0L, NA, NA, NA), 
whaleage3 = c(NA, NA, NA, NA, NA, 1L, NA, NA, 6L, NA, NA, 
8L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 6L, 2L, NA, 20L, 
20L, NA, NA, NA, NA, 2L, 4L, NA, 19L, NA, 30L, 33L, NA, 34L, 
13L, NA, 6L, 6L, NA, NA, 1L, NA, NA, NA, 20L, NA, NA, 15L, 
NA, NA, 7L, 5L, NA, NA, NA, NA, NA, NA, NA, NA, 24L, 1L, 
7L, NA, NA, 6L, NA, NA, 21L, NA, NA, NA), whaleid4 = c(NA, 
NA, NA, NA, "Lx", NA, "Lx", "Kx", NA, "Lx", NA, NA, NA, "Jx", 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "L101", "L101", 
NA, NA, NA, NA, NA, NA, "Lx", "L94", NA, "L82", "Lx", NA, 
"L111", NA, NA, "Kx", NA, NA, NA, NA, NA, NA, NA, "K35", 
NA, NA, "J42", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, "Jx", NA, NA, NA, NA, "J47", NA, NA, "Lx"
), whalesex4 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
1L, 1L, NA, NA, NA, NA, NA, NA, NA, 0L, NA, 0L, NA, NA, 0L, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, 0L, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 1L, NA, NA, NA), whaleage4 = c(NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, 3L, 3L, NA, NA, NA, NA, NA, NA, 
NA, 11L, NA, 17L, NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, 11L, NA, NA, 7L, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 10L, 
NA, NA, NA), whaleid5 = c(NA, NA, NA, NA, NA, NA, "Lx", "Kx", 
NA, NA, NA, NA, NA, "Jx", NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "L86", 
NA, NA, "L91", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, "Jx", NA, NA, NA, NA, "J49", NA, NA, NA), whalesex5 = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, 0L, NA, NA, 0L, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, 
NA, NA), whaleage5 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 16L, NA, 
NA, 13L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, 8L, NA, NA, NA), whaleid6 = c(NA, 
NA, NA, NA, NA, NA, "Lx", "Kx", NA, NA, NA, NA, NA, "Jx", 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, "L95", NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "Jx", NA, NA, 
NA, NA, NA, NA, NA, NA), whalesex6 = c(NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), whaleage6 = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, 11L, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA), whaleid7 = c(NA, NA, NA, NA, NA, NA, "Lx", "Kx", 
NA, NA, NA, NA, NA, "Jx", NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "L77", 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, "Jx", NA, NA, NA, NA, NA, NA, NA, NA), whalesex7 = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, 0L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA), whaleage7 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 20L, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), whaleid8 = c(NA, 
NA, NA, NA, NA, NA, "Lx", "Kx", NA, NA, NA, NA, NA, "Jx", 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, "L94", NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "Jx", NA, NA, 
NA, NA, NA, NA, NA, NA), whalesex8 = c(NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, 0L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), whaleage8 = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, 12L, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA), event.id = 1:78), row.names = c(NA, -78L), class = "data.frame")

我想使用 r_bind 按鲸鱼 ID 进行分组,同时将每条鲸鱼各自的年龄和性别归因于其 ID。我已经了解了这么多:

events.by.id = 
rbind(
srkw.dat %>%
  select(event.id, year, id = whaleid1),
srkw.dat %>%
  select(event.id, year, id = whaleid2),
srkw.dat %>%
  select(event.id, year, id = whaleid3),
srkw.dat %>%
  select(event.id, year, id = whaleid4),
srkw.dat %>%
  select(event.id, year, id = whaleid5),
srkw.dat %>%
  select(event.id, year, id = whaleid6),
srkw.dat %>%
  select(event.id, year, id = whaleid7)
) %>%
filter(!is.na(id))

但我不确定需要什么额外的语法来将每头鲸鱼的年龄和性别与其 ID 联系起来?

另外,完成之后,是否有一种优雅的方法来选择性地删除 Jx、Lx、Kx 等鲸鱼 ID(及其归属的性别/年龄)? 非常感谢!

My data look like this:

dput(srkw.dat)
structure(list(year = c(1962L, 1976L, 1981L, 1981L, 1982L, 
1987L, 1989L, 1990L, 1992L, 1992L, 1992L, 1994L, 1998L, 2003L, 
2003L, 2003L, 2003L, 2004L, 2004L, 2004L, 2005L, 2005L, 2005L, 
2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2006L, 2006L, 
2006L, 2006L, 2006L, 2006L, 2007L, 2007L, 2007L, 2008L, 2008L, 
2008L, 2009L, 2009L, 2009L, 2010L, 2010L, 2010L, 2011L, 2011L, 
2013L, 2013L, 2013L, 2014L, 2014L, 2014L, 2014L, 2014L, 2015L, 
2015L, 2015L, 2015L, 2015L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2017L, 2018L, 2018L, 2018L, 2019L, 2020L, 2020L, 2020L, 
2020L), whaleid1 = c("Lx", "Lx", "Lx", "Lx", "Lx", "L5", 
"Lx", "Kx", "L5", "Lx", "L21", "L5", "Lx", "Jx", "L5", "L7", 
"Jx", "L21", "L54", "Lx", "Jx", "J27", "J16", "J11", "Jx", 
"L5", "L5", "J30", "L95", "Lx", "Jx", "J35", "K40", "Lx", 
"L12", "Jx", "L106", "L21", "Kx", "L83", "L83", "L57", "J31", 
"J27", "J30", "L55", "L22", "K36", "Jx", "L72", "K21", NA, 
"K16", "J16", "J35", "L72", "J36", "L22", "K22", "K22", "L77", 
"J47", "Jx", "J40", "L119", "J2", "J35", "L103", "L77", "L87", 
"J39", "J22", "J38", "L115", "J31", NA, "J37", "Lx"), whalesex1 = c(NA, 
0L, NA, NA, 0L, 0L, NA, NA, 0L, 0L, 0L, 0L, 1L, NA, 0L, 0L, 
NA, 0L, 0L, NA, NA, 1L, 0L, 0L, NA, 0L, 0L, 1L, 1L, NA, NA, 
0L, 0L, NA, 0L, NA, 1L, 0L, NA, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 
0L, 0L, NA, 0L, 1L, NA, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
1L, NA, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, NA, 
0L, NA), whaleage1 = c(NA, NA, NA, NA, NA, 23L, NA, NA, 28L, 
NA, 42L, 30L, NA, NA, 39L, 42L, NA, 54L, 27L, NA, NA, 14L, 
33L, 33L, NA, 41L, 41L, 10L, 9L, NA, NA, 8L, 43L, NA, 73L, 
NA, 2L, 57L, NA, 18L, 18L, 31L, 14L, 18L, 14L, 33L, 39L, 
7L, NA, 25L, 27L, NA, 28L, 42L, 16L, 28L, 15L, 43L, 28L, 
28L, 28L, 5L, NA, 12L, 4L, 60L, 18L, 13L, 29L, 24L, 15L, 
23L, 15L, 9L, 25L, NA, 19L, NA), whaleid2 = c("Lx", "Lx", 
"Lx", "Lx", "Lx", "L58", "Lx", "Kx", "L58", "Lx", "Lx", "L58", 
NA, "Jx", "L73", "L57", "Jx", "L47", "L100", "Lx", NA, "J31", 
"J26", "J27", "Jx", "L73", "L73", "J31", "L72", "Lx", "Jx", 
"J37", "K16", "Lx", "L41", NA, "L41", "L26", NA, "L110", 
"L110", NA, "J36", "J31", "Kx", NA, "L94", "K42", NA, "Kx", 
"K25", NA, NA, "J26", "J47", "L105", "J41", "L94", NA, "K33", 
"L119", NA, NA, NA, NA, NA, "J47", "L109", "L119", "Jx", 
NA, "J40", NA, NA, "J56", NA, "Jx", "Lx"), whalesex2 = c(NA, 
NA, NA, NA, NA, 1L, NA, NA, 1L, 0L, NA, 1L, NA, NA, 1L, 1L, 
NA, 0L, 1L, NA, NA, 0L, 1L, 1L, NA, 1L, 1L, 0L, 0L, NA, NA, 
0L, 0L, NA, 1L, NA, 1L, 0L, NA, 1L, 1L, NA, 0L, 0L, NA, NA, 
0L, 1L, NA, NA, 1L, NA, NA, 1L, 1L, 1L, 0L, 0L, NA, 1L, 0L, 
NA, NA, NA, NA, NA, 1L, 1L, 0L, NA, NA, 0L, NA, NA, 0L, NA, 
NA, NA), whaleage2 = c(NA, NA, NA, NA, NA, 7L, NA, NA, 12L, 
NA, NA, 14L, NA, NA, 17L, 26L, NA, 30L, 3L, NA, NA, 10L, 
14L, 14L, NA, 19L, 19L, 10L, 19L, NA, NA, 5L, 21L, NA, 29L, 
NA, 30L, 51L, NA, 1L, 1L, NA, 10L, 14L, NA, NA, 15L, 2L, 
NA, NA, 22L, NA, NA, 23L, 4L, 10L, 9L, 19L, NA, 14L, 3L, 
NA, NA, NA, NA, NA, 6L, 9L, 4L, NA, NA, 14L, NA, NA, 1L, 
NA, NA, NA), whaleid3 = c(NA, NA, NA, NA, "Lx", "L73", "Lx", 
"Kx", "L73", "Lx", NA, "L73", NA, "Jx", NA, NA, NA, "Lx", 
NA, NA, NA, "Jx", "J36", "J39", NA, "L67", "L67", NA, NA, 
NA, NA, "J40", "K35", "Lx", "L77", NA, "L57", "L47", NA, 
"L47", "L91", NA, "J39", "J39", NA, NA, "L113", NA, NA, NA, 
"K26", NA, NA, "J36", NA, NA, "J42", "L113", NA, NA, NA, 
NA, NA, NA, NA, NA, "L87", "L123", "L113", "Jx", NA, "J49", 
NA, NA, "J36", NA, NA, "Lx"), whalesex3 = c(NA, NA, NA, NA, 
0L, 1L, NA, NA, 1L, NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, 0L, 1L, NA, 0L, 0L, NA, NA, NA, NA, 0L, 1L, NA, 
0L, NA, 1L, 0L, NA, 0L, 0L, NA, 1L, 1L, NA, NA, 0L, NA, NA, 
NA, 1L, NA, NA, 0L, NA, NA, 0L, 0L, NA, NA, NA, NA, NA, NA, 
NA, NA, 1L, 1L, 0L, 0L, NA, 1L, NA, NA, 0L, NA, NA, NA), 
whaleage3 = c(NA, NA, NA, NA, NA, 1L, NA, NA, 6L, NA, NA, 
8L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 6L, 2L, NA, 20L, 
20L, NA, NA, NA, NA, 2L, 4L, NA, 19L, NA, 30L, 33L, NA, 34L, 
13L, NA, 6L, 6L, NA, NA, 1L, NA, NA, NA, 20L, NA, NA, 15L, 
NA, NA, 7L, 5L, NA, NA, NA, NA, NA, NA, NA, NA, 24L, 1L, 
7L, NA, NA, 6L, NA, NA, 21L, NA, NA, NA), whaleid4 = c(NA, 
NA, NA, NA, "Lx", NA, "Lx", "Kx", NA, "Lx", NA, NA, NA, "Jx", 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "L101", "L101", 
NA, NA, NA, NA, NA, NA, "Lx", "L94", NA, "L82", "Lx", NA, 
"L111", NA, NA, "Kx", NA, NA, NA, NA, NA, NA, NA, "K35", 
NA, NA, "J42", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, "Jx", NA, NA, NA, NA, "J47", NA, NA, "Lx"
), whalesex4 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
1L, 1L, NA, NA, NA, NA, NA, NA, NA, 0L, NA, 0L, NA, NA, 0L, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, 0L, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 1L, NA, NA, NA), whaleage4 = c(NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, 3L, 3L, NA, NA, NA, NA, NA, NA, 
NA, 11L, NA, 17L, NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, 11L, NA, NA, 7L, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 10L, 
NA, NA, NA), whaleid5 = c(NA, NA, NA, NA, NA, NA, "Lx", "Kx", 
NA, NA, NA, NA, NA, "Jx", NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "L86", 
NA, NA, "L91", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, "Jx", NA, NA, NA, NA, "J49", NA, NA, NA), whalesex5 = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, 0L, NA, NA, 0L, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, 
NA, NA), whaleage5 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 16L, NA, 
NA, 13L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, 8L, NA, NA, NA), whaleid6 = c(NA, 
NA, NA, NA, NA, NA, "Lx", "Kx", NA, NA, NA, NA, NA, "Jx", 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, "L95", NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "Jx", NA, NA, 
NA, NA, NA, NA, NA, NA), whalesex6 = c(NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), whaleage6 = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, 11L, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA), whaleid7 = c(NA, NA, NA, NA, NA, NA, "Lx", "Kx", 
NA, NA, NA, NA, NA, "Jx", NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "L77", 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, "Jx", NA, NA, NA, NA, NA, NA, NA, NA), whalesex7 = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, 0L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA), whaleage7 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 20L, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), whaleid8 = c(NA, 
NA, NA, NA, NA, NA, "Lx", "Kx", NA, NA, NA, NA, NA, "Jx", 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, "L94", NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "Jx", NA, NA, 
NA, NA, NA, NA, NA, NA), whalesex8 = c(NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, 0L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), whaleage8 = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, 12L, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA), event.id = 1:78), row.names = c(NA, -78L), class = "data.frame")

I want to use r_bind to group by whale IDs, while ascribing each whale's respective age and sex to its ID. I got this far:

events.by.id = 
rbind(
srkw.dat %>%
  select(event.id, year, id = whaleid1),
srkw.dat %>%
  select(event.id, year, id = whaleid2),
srkw.dat %>%
  select(event.id, year, id = whaleid3),
srkw.dat %>%
  select(event.id, year, id = whaleid4),
srkw.dat %>%
  select(event.id, year, id = whaleid5),
srkw.dat %>%
  select(event.id, year, id = whaleid6),
srkw.dat %>%
  select(event.id, year, id = whaleid7)
) %>%
filter(!is.na(id))

But I wasn't sure what extra syntax is needed to link each whale's age and sex to its ID?

Additionally, after that is done, is there an elegant way to selectively remove the whale IDs (and their ascribed sexes/ages) that are Jx, Lx, Kx, etc?
Thanks so much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水染的天色ゝ 2025-01-17 18:33:11

我认为如果您使用 pivot_longeridsexage 单独获取长格式的数据,将会有所帮助列。这可能比您的尝试更简单。

res <- tidyr::pivot_longer(srkw.dat, 
                           cols = -year, 
                           names_to = c('.value', 'num'), 
                           names_pattern = 'whale(.*?)(\\d+)', 
                           values_drop_na = TRUE)
res

# A tibble: 211 x 5
#    year num   id      sex   age
#   <int> <chr> <chr> <int> <int>
# 1  1962 1     Lx       NA    NA
# 2  1962 2     Lx       NA    NA
# 3  1976 1     Lx        0    NA
# 4  1976 2     Lx       NA    NA
# 5  1981 1     Lx       NA    NA
# 6  1981 2     Lx       NA    NA
# 7  1981 1     Lx       NA    NA
# 8  1981 2     Lx       NA    NA
# 9  1982 1     Lx        0    NA
#10  1982 2     Lx       NA    NA
# … with 201 more rows

现在使用 res 过滤数据会很容易。例如,要删除 id 的 c('Jx', 'Lx', 'Kx') 你可以这样做 -

res %>% filter(!id %in% c('Jx', 'Lx', 'Kx'))

I think it would help if you get data in long format using pivot_longer with id, sex and age as separate columns. This might be simpler than your attempt.

res <- tidyr::pivot_longer(srkw.dat, 
                           cols = -year, 
                           names_to = c('.value', 'num'), 
                           names_pattern = 'whale(.*?)(\\d+)', 
                           values_drop_na = TRUE)
res

# A tibble: 211 x 5
#    year num   id      sex   age
#   <int> <chr> <chr> <int> <int>
# 1  1962 1     Lx       NA    NA
# 2  1962 2     Lx       NA    NA
# 3  1976 1     Lx        0    NA
# 4  1976 2     Lx       NA    NA
# 5  1981 1     Lx       NA    NA
# 6  1981 2     Lx       NA    NA
# 7  1981 1     Lx       NA    NA
# 8  1981 2     Lx       NA    NA
# 9  1982 1     Lx        0    NA
#10  1982 2     Lx       NA    NA
# … with 201 more rows

It would be easy now to filter data with res. For example, to drop id's c('Jx', 'Lx', 'Kx') you may do -

res %>% filter(!id %in% c('Jx', 'Lx', 'Kx'))
铃予 2025-01-17 18:33:11

您想要做的是透视您的数据。您实际上已经开发了自己的 tidyr::pivot_longer() 版本 - 但使用 Hadley 的版本会更容易。

对于第二个问题,您可以使用 stringr::str_ends() 按以“x”结尾的 id 进行过滤。

library(tidyverse)

srkw.dat_long <- srkw.dat %>% 
  pivot_longer(
    cols = starts_with("whale"),
    names_to = c(".value", NA),
    names_pattern = "whale(\\D+)(\\d+)"
  ) %>% 
  filter(!(is.na(id) | str_ends(id, "x")))

# # A tibble: 132 x 5
#     year event.id id      sex   age
#    <int>    <int> <chr> <int> <int>
#  1  1987        6 L5        0    23
#  2  1987        6 L58       1     7
#  3  1987        6 L73       1     1
#  4  1992        9 L5        0    28
#  5  1992        9 L58       1    12
#  6  1992        9 L73       1     6
#  7  1992       11 L21       0    42
#  8  1994       12 L5        0    30
#  9  1994       12 L58       1    14
# 10  1994       12 L73       1     8
# # ... with 122 more rows

What you want to do is pivot your data. You've actually kind of developed your own version of tidyr::pivot_longer() -- but it's easier with Hadley's version.

To your second question, you can use stringr::str_ends() to filter by ids ending in "x".

library(tidyverse)

srkw.dat_long <- srkw.dat %>% 
  pivot_longer(
    cols = starts_with("whale"),
    names_to = c(".value", NA),
    names_pattern = "whale(\\D+)(\\d+)"
  ) %>% 
  filter(!(is.na(id) | str_ends(id, "x")))

# # A tibble: 132 x 5
#     year event.id id      sex   age
#    <int>    <int> <chr> <int> <int>
#  1  1987        6 L5        0    23
#  2  1987        6 L58       1     7
#  3  1987        6 L73       1     1
#  4  1992        9 L5        0    28
#  5  1992        9 L58       1    12
#  6  1992        9 L73       1     6
#  7  1992       11 L21       0    42
#  8  1994       12 L5        0    30
#  9  1994       12 L58       1    14
# 10  1994       12 L73       1     8
# # ... with 122 more rows
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文