0

I am trying to subset a fairly large dataset (530 rows, 211 columns) into 2 subsets of 46 rows, 21 of 20 rows, 1 of 16 rows and 1 of 2 rows, using 'R'. Instead of copying the code several times, I would like to make a loop for this. Also, when subsetting, I want to delete the first 3 columns in the subset.

For subsetting I use this code:

Dataveg2018A <- subset(DatasetMerg, Year == "2018" & Block == "A",
                                      Select= 4:7)

Since I don't want to make a different code for every subset, I want to make use of a loop, but I can't figure out how to code this.

I have three different "year" categories, and for each year I have blocks A:J. I want to subset the entire dataset in blocks per year and use a loop for this.

Below I posted the code (using function dput) for a small part of the dataset. Excuse me for still making it tediously large, but I could not figure out how to compress it.

So, I need a code that loops the subset command above for each Block per year, resulting in a total of 25 subsets. In the compressed dataset below, it should result in 3 subsets (1 2018 Block A, one 2008 Block A and one 2001 Block A.

I hope I am clear enough.

structure(list(Year = structure(c(`18A01` = 3L, `18A02` = 3L, 
`18A03` = 3L, `18A04` = 3L, `18A05` = 3L, `18A06` = 3L, `18A07` = 3L, 
`18A08` = 3L, `18A09` = 3L, `18A10` = 3L, `18A11` = 3L, `18A12` = 3L, 
`18A13` = 3L, `18A14` = 3L, `18A15` = 3L, `18A16` = 3L, `18A17` = 3L, 
`18A18` = 3L, `18A19` = 3L, `18A20` = 3L, `08A01` = 2L, `08A02` = 2L, 
`08A03` = 2L, `08A04` = 2L, `08A05` = 2L, `08A06` = 2L, `08A07` = 2L, 
`08A08` = 2L, `08A09` = 2L, `08A10` = 2L, `08A11` = 2L, `08A12` = 2L, 
`08A13` = 2L, `08A14` = 2L, `08A15` = 2L, `08A16` = 2L, `08A17` = 2L, 
`08A18` = 2L, `08A19` = 2L, `08A20` = 2L, `01A01` = 1L, `01A02` = 1L, 
`01A03` = 1L, `01A04` = 1L, `01A05` = 1L, `01A06` = 1L, `01A07` = 1L, 
`01A08` = 1L, `01A09` = 1L, `01A10` = 1L, `01A11` = 1L, `01A12` = 1L, 
`01A13` = 1L, `01A14` = 1L, `01A15` = 1L, `01A16` = 1L, `01A17` = 1L, 
`01A18` = 1L, `01A19` = 1L, `01A20` = 1L), .Label = c("2001", 
"2008", "2018"), class = "factor"), Block = structure(c(`18A01` = 1L, 
`18A02` = 1L, `18A03` = 1L, `18A04` = 1L, `18A05` = 1L, `18A06` = 1L, 
`18A07` = 1L, `18A08` = 1L, `18A09` = 1L, `18A10` = 1L, `18A11` = 1L, 
`18A12` = 1L, `18A13` = 1L, `18A14` = 1L, `18A15` = 1L, `18A16` = 1L, 
`18A17` = 1L, `18A18` = 1L, `18A19` = 1L, `18A20` = 1L, `08A01` = 1L, 
`08A02` = 1L, `08A03` = 1L, `08A04` = 1L, `08A05` = 1L, `08A06` = 1L, 
`08A07` = 1L, `08A08` = 1L, `08A09` = 1L, `08A10` = 1L, `08A11` = 1L, 
`08A12` = 1L, `08A13` = 1L, `08A14` = 1L, `08A15` = 1L, `08A16` = 1L, 
`08A17` = 1L, `08A18` = 1L, `08A19` = 1L, `08A20` = 1L, `01A01` = 1L, 
`01A02` = 1L, `01A03` = 1L, `01A04` = 1L, `01A05` = 1L, `01A06` = 1L, 
`01A07` = 1L, `01A08` = 1L, `01A09` = 1L, `01A10` = 1L, `01A11` = 1L, 
`01A12` = 1L, `01A13` = 1L, `01A14` = 1L, `01A15` = 1L, `01A16` = 1L, 
`01A17` = 1L, `01A18` = 1L, `01A19` = 1L, `01A20` = 1L), .Label = c("A", 
"B", "C", "D", "E", "F", "G", "H", "I", "J", "X"), class = "factor"), 
Plot = structure(c(`18A01` = 1L, `18A02` = 2L, `18A03` = 3L, 
`18A04` = 4L, `18A05` = 5L, `18A06` = 6L, `18A07` = 7L, `18A08` = 8L, 
`18A09` = 9L, `18A10` = 10L, `18A11` = 11L, `18A12` = 12L, 
`18A13` = 13L, `18A14` = 14L, `18A15` = 15L, `18A16` = 16L, 
`18A17` = 17L, `18A18` = 18L, `18A19` = 19L, `18A20` = 20L, 
`08A01` = 1L, `08A02` = 2L, `08A03` = 3L, `08A04` = 4L, `08A05` = 5L, 
`08A06` = 6L, `08A07` = 7L, `08A08` = 8L, `08A09` = 9L, `08A10` = 10L, 
`08A11` = 11L, `08A12` = 12L, `08A13` = 13L, `08A14` = 14L, 
`08A15` = 15L, `08A16` = 16L, `08A17` = 17L, `08A18` = 18L, 
`08A19` = 19L, `08A20` = 20L, `01A01` = 1L, `01A02` = 2L, 
`01A03` = 3L, `01A04` = 4L, `01A05` = 5L, `01A06` = 6L, `01A07` = 7L, 
`01A08` = 8L, `01A09` = 9L, `01A10` = 10L, `01A11` = 11L, 
`01A12` = 12L, `01A13` = 13L, `01A14` = 14L, `01A15` = 15L, 
`01A16` = 16L, `01A17` = 17L, `01A18` = 18L, `01A19` = 19L, 
`01A20` = 20L), .Label = c(" 1", " 2", " 3", " 4", " 5", 
" 6", " 7", " 8", " 9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "21", "22", "23", "24", "25", 
"26", "27", "28", "29", "30", "31", "32", "33", "34", "35", 
"36", "37", "38", "39", "40", "41", "42", "43", "44", "45", 
"46"), class = "factor"), Agrimonia.eupatoria = structure(c(`18A01` = 3L, 
`18A02` = 2L, `18A03` = 1L, `18A04` = 1L, `18A05` = 1L, `18A06` = 1L, 
`18A07` = 1L, `18A08` = 1L, `18A09` = 1L, `18A10` = 1L, `18A11` = 1L, 
`18A12` = 1L, `18A13` = 1L, `18A14` = 1L, `18A15` = 1L, `18A16` = 1L, 
`18A17` = 1L, `18A18` = 1L, `18A19` = 1L, `18A20` = 1L, `08A01` = 1L, 
`08A02` = 1L, `08A03` = 1L, `08A04` = 1L, `08A05` = 1L, `08A06` = 1L, 
`08A07` = 1L, `08A08` = 1L, `08A09` = 1L, `08A10` = 1L, `08A11` = 1L, 
`08A12` = 1L, `08A13` = 1L, `08A14` = 1L, `08A15` = 1L, `08A16` = 1L, 
`08A17` = 1L, `08A18` = 1L, `08A19` = 1L, `08A20` = 1L, `01A01` = 3L, 
`01A02` = 1L, `01A03` = 1L, `01A04` = 1L, `01A05` = 1L, `01A06` = 1L, 
`01A07` = 1L, `01A08` = 1L, `01A09` = 1L, `01A10` = 1L, `01A11` = 1L, 
`01A12` = 1L, `01A13` = 1L, `01A14` = 1L, `01A15` = 1L, `01A16` = 1L, 
`01A17` = 1L, `01A18` = 1L, `01A19` = 1L, `01A20` = 1L), .Label = c("0", 
"1", "2", "6"), class = "factor"), Agrostis.capillaris = structure(c(`18A01` 
= 1L, 
`18A02` = 1L, `18A03` = 1L, `18A04` = 1L, `18A05` = 1L, `18A06` = 1L, 
`18A07` = 1L, `18A08` = 1L, `18A09` = 1L, `18A10` = 1L, `18A11` = 1L, 
`18A12` = 1L, `18A13` = 1L, `18A14` = 1L, `18A15` = 1L, `18A16` = 1L, 
`18A17` = 1L, `18A18` = 1L, `18A19` = 1L, `18A20` = 1L, `08A01` = 1L, 
`08A02` = 1L, `08A03` = 1L, `08A04` = 1L, `08A05` = 1L, `08A06` = 1L, 
`08A07` = 1L, `08A08` = 1L, `08A09` = 1L, `08A10` = 1L, `08A11` = 1L, 
`08A12` = 1L, `08A13` = 1L, `08A14` = 1L, `08A15` = 1L, `08A16` = 1L, 
`08A17` = 1L, `08A18` = 1L, `08A19` = 1L, `08A20` = 1L, `01A01` = 1L, 
`01A02` = 1L, `01A03` = 1L, `01A04` = 1L, `01A05` = 1L, `01A06` = 1L, 
`01A07` = 1L, `01A08` = 1L, `01A09` = 1L, `01A10` = 1L, `01A11` = 1L, 
`01A12` = 1L, `01A13` = 1L, `01A14` = 1L, `01A15` = 1L, `01A16` = 1L, 
`01A17` = 1L, `01A18` = 1L, `01A19` = 1L, `01A20` = 1L), .Label = c("0", 
"2", "3", "4", "6"), class = "factor"), Agrostis.gigantea = 
structure(c(`18A01` = 1L, 
`18A02` = 1L, `18A03` = 1L, `18A04` = 1L, `18A05` = 1L, `18A06` = 1L, 
`18A07` = 1L, `18A08` = 1L, `18A09` = 1L, `18A10` = 1L, `18A11` = 2L, 
`18A12` = 3L, `18A13` = 2L, `18A14` = 1L, `18A15` = 3L, `18A16` = 3L, 
`18A17` = 4L, `18A18` = 3L, `18A19` = 1L, `18A20` = 1L, `08A01` = 1L, 
`08A02` = 1L, `08A03` = 1L, `08A04` = 1L, `08A05` = 1L, `08A06` = 1L, 
`08A07` = 1L, `08A08` = 1L, `08A09` = 1L, `08A10` = 1L, `08A11` = 1L, 
`08A12` = 1L, `08A13` = 1L, `08A14` = 1L, `08A15` = 1L, `08A16` = 1L, 
`08A17` = 1L, `08A18` = 1L, `08A19` = 1L, `08A20` = 1L, `01A01` = 1L, 
`01A02` = 1L, `01A03` = 1L, `01A04` = 1L, `01A05` = 1L, `01A06` = 1L, 
`01A07` = 1L, `01A08` = 1L, `01A09` = 1L, `01A10` = 1L, `01A11` = 1L, 
`01A12` = 1L, `01A13` = 1L, `01A14` = 1L, `01A15` = 1L, `01A16` = 1L, 
`01A17` = 1L, `01A18` = 1L, `01A19` = 1L, `01A20` = 1L), .Label = c("0", 
"2", "3", "4", "6"), class = "factor"), Agrostis.stolonifera = 
structure(c(`18A01` = 5L, 
`18A02` = 1L, `18A03` = 1L, `18A04` = 1L, `18A05` = 1L, `18A06` = 1L, 
`18A07` = 1L, `18A08` = 1L, `18A09` = 1L, `18A10` = 1L, `18A11` = 1L, 
`18A12` = 1L, `18A13` = 1L, `18A14` = 1L, `18A15` = 1L, `18A16` = 1L, 
`18A17` = 1L, `18A18` = 1L, `18A19` = 1L, `18A20` = 1L, `08A01` = 5L, 
`08A02` = 1L, `08A03` = 1L, `08A04` = 1L, `08A05` = 1L, `08A06` = 1L, 
`08A07` = 1L, `08A08` = 1L, `08A09` = 1L, `08A10` = 1L, `08A11` = 1L, 
`08A12` = 1L, `08A13` = 5L, `08A14` = 1L, `08A15` = 1L, `08A16` = 1L, 
`08A17` = 1L, `08A18` = 1L, `08A19` = 1L, `08A20` = 1L, `01A01` = 1L, 
`01A02` = 1L, `01A03` = 1L, `01A04` = 1L, `01A05` = 1L, `01A06` = 1L, 
`01A07` = 1L, `01A08` = 1L, `01A09` = 1L, `01A10` = 1L, `01A11` = 1L, 
`01A12` = 1L, `01A13` = 1L, `01A14` = 1L, `01A15` = 1L, `01A16` = 1L, 
`01A17` = 1L, `01A18` = 1L, `01A19` = 1L, `01A20` = 1L), .Label = c("0", 
"1", "18", "2", "3", "38", "4", "6", "68", "8"), class = "factor")), 
row.names = c("18A01", 
"18A02", "18A03", "18A04", "18A05", "18A06", "18A07", "18A08", 
"18A09", "18A10", "18A11", "18A12", "18A13", "18A14", "18A15", 
"18A16", "18A17", "18A18", "18A19", "18A20", "08A01", "08A02", 
"08A03", "08A04", "08A05", "08A06", "08A07", "08A08", "08A09", 
"08A10", "08A11", "08A12", "08A13", "08A14", "08A15", "08A16", 
"08A17", "08A18", "08A19", "08A20", "01A01", "01A02", "01A03", 
"01A04", "01A05", "01A06", "01A07", "01A08", "01A09", "01A10", 
"01A11", "01A12", "01A13", "01A14", "01A15", "01A16", "01A17", 
"01A18", "01A19", "01A20"), class = "data.frame")
Stevestingray
  • 399
  • 2
  • 12
  • 2
    `Select` should be `select` and you need to pass either the index or column numbers i.e. `select = 4:211` – akrun Feb 19 '19 at 11:24
  • 1
    Please, provide an [example of your data](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), not a screenshot. – patL Feb 19 '19 at 11:25
  • 1
    pls provide reproducible example – Hunaidkhan Feb 19 '19 at 11:25
  • Thanks for the responses. I will make an example as soon as possible. I am not familiar with this, so I don't know how to create these on the fly. – Stevestingray Feb 19 '19 at 12:33

1 Answers1

0

Something like this may get you what you're after. It will run the subset for each year and block, and store the results in a named list.

lst.subsets <- list()
subset.iterator <- 0

# Get all possible years from dataframe
years <- unique(DatasetMerg$Year)

for(y in years){
  # Get all possible blocks for the current year
  blocks <- unique(DatasetMerg[DatasetMerg$Year == y, "Block"])

  for(b in blocks){
    subset.iterator <- subset.iterator + 1
    lst.subsets[[subset.iterator]] <- subset(DatasetMerg, 
                                             Year == y & Block == b,
                                             select = c(4:7))

    names(lst.subsets)[[subset.iterator]] <- paste0('Dataveg', y, b)
  }
}
Matt Jewett
  • 3,249
  • 1
  • 14
  • 21