Creating a count per year

Hello,

I'm trying to count the number of partners an individual has each year and can't quite manage it.
Here is the dput:
df1 <- structure(list(monkey.id = c("00J", "00J", "00J", "00J", "00J",
"00J", "00J", "00J", "00J", "00J", "00J", "00J", "00J", "00J",
"00J", "00J", "00J", "00J", "00J", "00O", "00O", "00O", "00O",
"00O", "00O", "00O", "00O", "00O", "00O", "00O", "00O", "00O",
"00O", "00O", "00O", "00O", "00O", "00O", "00O", "00O", "00O",
"00O", "00O", "00O", "00O", "00O", "00O", "00O", "00O", "00O",
"00O", "00O", "00O", "00O", "00O", "00O", "00O", "00O", "00O",
"00O", "00O", "00O", "00O", "00O", "00O", "00O", "00O", "00O",
"00O", "00O", "00O", "00O", "00O", "00O", "00O", "00O", "00O",
"00V", "00V", "00V", "00V", "00V", "00V", "03J", "03J", "03J",
"03J", "03J", "03J", "03J", "03J", "03J", "03J", "03J", "03J",
"03J", "03J", "03J", "03J", "03J", "03J", "03J", "03J", "03J",
"03J", "03J", "03J", "03J", "03J", "03J", "03J", "03J", "03J",
"03J", "04N", "04N", "04N", "04N", "04N", "04N", "04N", "04N",
"04N", "04N", "04N", "04N", "04N", "04N", "04N", "04N", "04N",
"04N", "04N", "04N", "04N", "04N", "04N", "04N", "04N", "04N",
"04N", "04N", "04N", "04N", "04N", "04N", "04N", "04N", "04N",
"04N"), partner.id = c("03J", "03J", "03J", "03J", "0A9", "0A9",
"0A9", "35N", "35N", "43J", "43J", "60P", "60P", "82L", "82L",
"93Z", "93Z", "9A4", "9A4", "24B", "24B", "28N", "28N", "29Z",
"29Z", "2A7", "2A7", "2A7", "2A7", "2A8", "2A8", "32S", "32S",
"32S", "32S", "34N", "34N", "34N", "34N", "36Z", "36Z", "43J",
"43J", "44J", "44J", "55V", "55V", "55V", "55V", "56E", "56E",
"57H", "57H", "5G9", "5G9", "61J", "61J", "61J", "61J", "61J",
"62H", "62H", "62H", "6C0", "6C0", "78T", "78T", "79Z", "79Z",
"82A", "82A", "8B0", "8B0", "98T", "98T", "N12", "N12", "1B0",
"1B0", "78I", "78I", "9C1", "9C1", "00J", "00J", "00J", "00J",
"0C6", "0C6", "0C6", "42L", "42L", "42L", "44J", "44J", "4C2",
"4C2", "56A", "56A", "62V", "62V", "62V", "63V", "63V", "64D",
"64D", "74S", "74S", "79C", "79C", "90D", "90D", "X44", "X44",
"0A4", "0A4", "1C0", "1C0", "1C0", "24B", "24B", "29Z", "29Z",
"2A2", "2A2", "32S", "32S", "43J", "43J", "43J", "43J", "43J",
"4C1", "4C1", "4C1", "57H", "57H", "57H", "59A", "59A", "59A",
"5C0", "5C0", "60P", "60P", "60P", "62H", "62H", "62H", "63V"
), data.year = c(2010L, 2011L, 2012L, 2014L, 2011L, 2015L, 2016L,
2010L, 2016L, 2010L, 2012L, 2010L, 2011L, 2011L, 2013L, 2011L,
2014L, 2013L, 2016L, 2011L, 2014L, 2010L, 2011L, 2010L, 2016L,
2011L, 2013L, 2014L, 2017L, 2011L, 2013L, 2010L, 2011L, 2012L,
2016L, 2010L, 2011L, 2013L, 2014L, 2010L, 2011L, 2010L, 2011L,
2010L, 2011L, 2010L, 2011L, 2012L, 2013L, 2012L, 2014L, 2010L,
2015L, 2015L, 2016L, 2010L, 2011L, 2012L, 2013L, 2014L, 2010L,
2013L, 2016L, 2012L, 2014L, 2011L, 2016L, 2011L, 2013L, 2012L,
2013L, 2012L, 2017L, 2010L, 2011L, 2010L, 2011L, 2015L, 2016L,
2015L, 2016L, 2015L, 2016L, 2010L, 2011L, 2012L, 2014L, 2012L,
2013L, 2014L, 2012L, 2013L, 2014L, 2010L, 2011L, 2013L, 2014L,
2010L, 2011L, 2010L, 2011L, 2012L, 2010L, 2011L, 2011L, 2014L,
2011L, 2013L, 2010L, 2011L, 2010L, 2013L, 2010L, 2011L, 2011L,
2014L, 2014L, 2015L, 2016L, 2010L, 2011L, 2010L, 2011L, 2011L,
2017L, 2012L, 2014L, 2010L, 2012L, 2013L, 2014L, 2015L, 2013L,
2014L, 2016L, 2010L, 2011L, 2012L, 2010L, 2011L, 2017L, 2013L,
2017L, 2010L, 2011L, 2012L, 2010L, 2011L, 2016L, 2010L)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -150L), groups = structure(list(
monkey.id = c("00J", "00O", "00V", "03J", "04N"), .rows = structure(list(
1:19, 20:77, 78:83, 84:114, 115:150), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -5L), .drop = TRUE))

What I want to do is count how many partners each individual monkey has in each year. Any help is much appreciated.

library(tidyverse)


df1 |> 
  group_by(monkey.id,data.year) |> distinct() |>
  count()


# same but also wider

df1 |> 
  group_by(monkey.id,data.year) |> distinct() |>
  count() |> pivot_wider(names_from = "data.year",values_from = "n")

Thank you, is there anyway I could make the count into a variable?

You can use <- the asignment operator to store a result into a variable.

df2 <- df1 |> 
  group_by(monkey.id,data.year) |> distinct() |>
  count()
2 Likes

Thanks very much for your help

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.