1

Currently, I am doing this. It subsets data from an aggregate. Running the part aggregate[which(aggregate$resolution == keys[i]), ] gives me the part of the aggregate data set that I want. However, when I attempt to do this in a for loop, it gives me a pile of key strings.

Why does this happen?

How can I get the subset to be put into the list?

keys <- c()
keys[1] <- "2016_01_12_ban_on_booby-trapped_aid.tsv"              
keys[2] <- "2014_11_20_anti-counterfeiting_pact.tsv"              
keys[3] <- "2015_08_06_access_to_humanitarian_aid.tsv"            
keys[4] <- "2017_03_22_world_assembly_central_library_compact.tsv"
keys[5] <- "2017_08_31_national_sovereignty_act.tsv"              
keys[6] <- "2015_10_21_repeal_renewable_research_commitment.tsv"  
keys[7] <- "2017_07_29_limitations_on_banishment.tsv"             
keys[8] <- "2016_10_04_nuclear_weapons_accord.tsv"                
keys[9] <- "2017_03_07_international_patent_agreement.tsv"        
keys[10] <- "2016_04_30_repeal_law_enforcement_education.tsv"

subsets <- list()
for (i in 1:length(keys)) {
  subsets[i] = aggregate[which(aggregate$resolution == keys[i]), ]
}
ifly6
  • 5,003
  • 2
  • 24
  • 47
  • Maybe `subsets <- split(aggregate[aggregate$resolution %in% keys,], keys)`. Note that `aggregate` is a base R function. It is difficult to say for sure without a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example-aka-mcve-minimal-complete-and-ver). – lmo Nov 30 '17 at 18:54
  • Changed the name to something else, problem persists. Tried split function as recommended, there is an order of each element in the data set, it pulls every 10th element. – ifly6 Nov 30 '17 at 19:11
  • Not sure what this means without looking at the data. Please include small example that reproduces your problem. – lmo Nov 30 '17 at 19:18
  • Running `aggregate[which(aggregate$resolution == keys[i]), ]` gives the correct subset of the data – ifly6 Nov 30 '17 at 19:21

1 Answers1

0

Solved problem.

keys <- unique(aggregate$resolution)
subsets = list()
for (i in 1:length(keys)) {
  subsets[[i]] = as.data.frame(main_data_set[which(main_data_set$resolution == keys[i]), ])
}
ifly6
  • 5,003
  • 2
  • 24
  • 47