I am trying to figure out how to create new vectors conditional on levels in a dataframe. For example:
Let's say I have a dataframe with a column of sales territories labeled A through D with United States Zipcodes (postal codes) attached associated with each.
For example: the structure of my data is setup as follows:
territories <- c('A','A','A','B','B','B','C','C','C','D','D','D') zips <- c('60601','60602','60603','60604','85718','85719','85720','32605','32606','32607','80504','80505','80506') df <- data.frame(territories,zips) df
I would like to create separate vectors for each for the territories that contain the associated zipcodes. For example :
terr_A <- filter(df, Terr.Code == 'A') terr_A <- terr_A[ ,"zips"]
For any one sales territory this would product a vector containing the zipcodes in that territory. I would like to repeat this for each of my territories. Perhaps via a for loop that creates the new vector with a name based on the territory, and repeats until all of the territories in the dataset have been completed.
terr_B, terr_C, terr_D etc.