I have searched through the archives and to no avail on this problem I have involving the subsetting of 2 related data frames, one data frame is a key, the other is an annual list, I'd like to use the key to create a subset and an index. I have tried using the subset formula's but my code is not appropriately meeting my criteria. Here is the data:
players <- c('Albert Belle','Reggie Jackson', 'Reggie Jackson')
contract_start_season <- c(1999,1977,1982)
contract_end_season <- c(2003, 1981, 1985)
key <- data.frame (player = players, contract_start_season, contract_end_season)
player_data <- data.frame( season = c(seq(1975,1985),seq(1997,2003)), player = c(rep('Reggie Jackson',times=11),rep('Albert Belle', times=7)))
I want to use the key to subset the player data to those years, so for Jackson 1977 to 1981 and then 1982 to 1985 and for Albert Belle 1999 to 2003. I'd also like to create an index so for example Reggie Jackson 1977 would be year 1, 1978 year 2 ect...
The code I have tried without merging looks like this and it isn't working:
player_data[player_data$season >= key$contract_start_season&player_data$season <= key$contract_end_season,]
I am also running into problems when merging because Reggie Jackson has 2 different contract years and it is trying to merge both.
Any help or advice on this would be super appreciated.