If you are dealing with only two people, you can leave with the subset separately, and then rbind for each subset:
wloc08F07001 <- wloc08[which(wloc08$ID == "F07001")[1:500], ] wloc08F07005 <- wloc08[which(wloc08$ID == "F07005")[1:500], ] reduced <- rbind(wloc08F07001, wloc08F07005)
To make this more general, especially if you are dealing with large amounts of data, you can consider the data.table package. Here is an example
library(data.table) wloc08DT<-as.data.table(wloc08) # Create data.table setkey(wloc08DT, "ID") # Set a key to subset on # EDIT: A comment from Matthew Dowle pointed out that by = "ID" isn't necessary # reduced <- wloc08DT[c("F07001", "F07005"), .SD[1:500], by = "ID"] reduced <- wloc08DT[c("F07001", "F07005"), .SD[1:500]]
To break down the syntax of the last step:
c("F07001", "F07005") : this will multiply your data by finding all the rows where the key is F07001 or F07005 . It will also initiate "no help" (see ?data.table for details)
.SD[1:500] : This will multiply the .SD object (a subset of the .table data) by selecting rows 1: 500.
EDIT This piece has been removed thanks to a fix by Matthew Dole. "By by by by" is initiated by step 1. Previously: ( by = "ID" : This tells [.data.table to perform the operation in step 2 for each identifier separately, in this case only the identifiers specified in step 1.)
source share