如何使用 r_bind 和 select 对多个变量进行分组?
我的数据如下所示:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为如果您使用
pivot_longer
与id
、sex
和age
单独获取长格式的数据,将会有所帮助列。这可能比您的尝试更简单。现在使用
res
过滤数据会很容易。例如,要删除 id 的c('Jx', 'Lx', 'Kx')
你可以这样做 -I think it would help if you get data in long format using
pivot_longer
withid
,sex
andage
as separate columns. This might be simpler than your attempt.It would be easy now to filter data with
res
. For example, to drop id'sc('Jx', 'Lx', 'Kx')
you may do -您想要做的是透视您的数据。您实际上已经开发了自己的
tidyr::pivot_longer()
版本 - 但使用 Hadley 的版本会更容易。对于第二个问题,您可以使用
stringr::str_ends()
按以“x”结尾的id
进行过滤。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 byid
s ending in "x".