How to subset multiple groups in R

Hi,
I'm fairly new to R, and have a simple problem that I can't seem to solve.
I have a variable called "data.year", I want to make a subset of the years 2015 and 2016.
Can someone help me do that please?
Here is the dput:
df1 <- structure(list(data.year = c(2015L, 2015L, 2015L, 2015L, 2015L,
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L,
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L,
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L,
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L,
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L,
2015L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L,
2016L, 2016L, 2016L, 2016L, 2016L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L,
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L), current.group = c("V",
"V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V",
"V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V",
"V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V",
"V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V",
"V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V",
"V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V",
"V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V",
"V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V",
"V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V",
"V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V",
"V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V",
"V", "V", "V", "V"), monkey.id = c("0F0", "0F0", "0F0", "14E",
"1B0", "1G0", "1G0", "1G0", "1G1", "1G2", "1G2", "1G2", "1G4",
"1G6", "2E4", "2E4", "2G2", "2G2", "2G4", "3H0", "3H0", "45Z",
"45Z", "45Z", "47Z", "47Z", "4C5", "4C5", "4C9", "4C9", "51Z",
"51Z", "51Z", "52Z", "5C6", "5C6", "5E8", "5E8", "78I", "78I",
"7C4", "7C4", "81E", "81E", "90T", "90T", "90T", "93T", "93T",
"9E8", "9E8", "0F0", "0F0", "0K0", "0K0", "13H", "14E", "15E",
"15E", "1G0", "1G0", "1G0", "1G1", "1G1", "1G6", "1I5", "1I5",
"1I7", "1I7", "1K1", "1K1", "1K2", "1K2", "2E4", "2E4", "2E4",
"2G2", "2G2", "2G2", "2G4", "2I3", "3D9", "3H0", "3H0", "47Z",
"4C5", "4C5", "4C9", "4C9", "4J4", "4J4", "51Z", "51Z", "53Z",
"5C6", "5E8", "78I", "7C4", "7C4", "90T", "90T", "93T", "93T",
"98J", "9C1", "9C1", "9C1", "9E8", "T03", "0F0", "0F0", "0K0",
"0M3", "0M4", "15E", "1G1", "1G1", "1G1", "1G4", "1I6", "1I6",
"1I7", "1I7", "1K1", "2E4", "2E4", "2G2", "2G2", "2K1", "4C5",
"4J4", "4J4", "52Z", "5E8", "5E8", "5L7", "5L7", "78I", "85T",
"90T", "90T", "93T", "93T", "9C1", "9C1", "9C1", "9E8", "9E8"
), partner.id = c("1G2", "2E4", "5E8", "1G2", "00V", "3H0", "4C9",
"90T", "7C4", "0F0", "14E", "1G1", "3H0", "7C4", "5C6", "7C4",
"2G4", "4C5", "2G2", "1G0", "1G4", "5E8", "00V", "2E4", "4C9",
"93T", "4C9", "2G2", "3H0", "47Z", "5C6", "2E4", "45Z", "2E4",
"7C4", "2E4", "0F0", "45Z", "00V", "2G4", "2E4", "5C6", "90T",
"9E8", "1G0", "3H0", "81E", "2G2", "47Z", "7C4", "81E", "0K0",
"2E4", "1K1", "0F0", "51Z", "51Z", "2E4", "5C6", "1G4", "1I5",
"4J4", "1I7", "3H0", "4J4", "00V", "1G0", "2G2", "00V", "2E4",
"0K0", "90T", "1G6", "7C4", "0F0", "1K1", "2I3", "90T", "1I7",
"5C6", "4J4", "52Z", "1G1", "2G4", "53Z", "5E8", "7C4", "93T",
"15E", "52Z", "2I3", "5C6", "14E", "47Z", "51Z", "00V", "00V",
"2E4", "5C6", "93T", "2G2", "1G6", "90T", "90T", "00V", "2G4",
"53Z", "00V", "1I6", "0K0", "2E4", "0F0", "0F0", "1K1", "93T",
"1I7", "7C4", "93T", "51Z", "85T", "1G4", "78I", "1G1", "0M4",
"5E8", "0F0", "4J4", "1I5", "5E8", "5L7", "1G4", "2G2", "5L7",
"1G4", "2E4", "1I5", "4C5", "9E8", "1I6", "93T", "2G2", "5E8",
"90T", "0F0", "4C5", "53Z", "4C9", "78I")), row.names = c(NA,
-148L), class = c("tbl_df", "tbl", "data.frame"))

There are many ways to do this. One possibility is

library(dplyr)
df1_sub <- df1 |> filter(data.year %in% c(2015,2016))
1 Like

Thanks very much, that worked great.

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.