Thanks so much for your help, really appreciate it! That worked really nicely.
Sorry to keep asking for help, but I have one final thing i'd like to do with the data and can't figure out how to do it, would you be able to help?
So I ran the code, and it created a new dataset with an individuals top 3 partners and their ranks (Thank you). I then created a binary variable, to code if any of their top 3 partners are non-kin, i.e 0 for kin and 1 for non-kin.
What I now want to do is know how many of of their top 3 partners are non-kin. So do a count, for each individual in each year, how many of their top 3 partners were non-kin. But I can't figure out how to do it.
This is the dput code:
df3 <- structure(list(groupyear = c("F2010", "F2010", "F2010", "F2010",
"F2010", "F2010", "F2010", "F2010", "F2010", "F2010", "F2010",
"F2010", "F2010", "F2010", "F2010", "F2010", "F2010", "F2010",
"F2010", "F2010"), monkey.id = c("00J", "00J", "00J", "00O",
"00O", "00O", "03J", "03J", "03J", "04N", "04N", "04N", "10S",
"10S", "10S", "14I", "14I", "14I", "14L", "14L"), partner.id = c("36Z",
"62V", "63V", "44J", "55V", "90D", "61J", "62V", "00J", "17C",
"24B", "98T", "14L", "64P", "68V", "29Z", "78T", "V36", "17C",
"68V"), data.year = c(2010L, 2010L, 2010L, 2010L, 2010L, 2010L,
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L,
2010L, 2010L, 2010L, 2010L, 2010L), current.group = c("F", "F",
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F",
"F", "F", "F", "F", "F"), DCSI = c(9.14323496669351, 5.24727433818152,
15.0677748544049, 13.3320473141112, 12.270117025579, 71.2592375296376,
15.6726142511153, 3.99126543701154, 1.27636402820631, 24.5036860857084,
7.72561755609198, 16.2382372788447, 18.5164970827263, 17.3079133497021,
15.9792558738643, 13.8380007377055, 40.1022560899402, 15.9123236938435,
14.3415425751191, 16.1986973817819), kinship = c("kin", "kin",
"nonkin", "nonkin", "nonkin", "kin", "kin", "nonkin", "nonkin",
"kin", "nonkin", "kin", "kin", "nonkin", "nonkin", "nonkin",
"kin", "nonkin", "nonkin", "nonkin"), partner.rank = c(2L, 3L,
1L, 2L, 3L, 1L, 1L, 2L, 3L, 1L, 3L, 2L, 1L, 2L, 3L, 3L, 1L, 2L,
3L, 2L), nonkinpresence = c("0", "0", "1", "1", "1", "0", "0",
"1", "1", "0", "1", "0", "0", "1", "1", "1", "0", "1", "1", "1"
)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA,
-20L), groups = structure(list(monkey.id = c("00J", "00O", "03J",
"04N", "10S", "14I", "14L"), groupyear = c("F2010", "F2010",
"F2010", "F2010", "F2010", "F2010", "F2010"), .rows = structure(list(
1:3, 4:6, 7:9, 10:12, 13:15, 16:18, 19:20), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -7L), .drop = TRUE))
Do you know how I might do this?
(edit: Have just realised that I didn't need to create the binary variable "nonkinpresence", as "kinship" is exactly the same thing).