For each row in an input data frame I want to create a new data frame consisting of all the rows in my data with the same RETAIL_WEEK value and DAY_OF_WEEK value. For instance, if I had matchday = matchweek = 3, the following can be used to find the desired data frame:
library(sqldf); library(gsubfn) # second one may not be needed.
fn$sqldf("select * from data where RETAIL_WEEK = $matchday and DAY_OF_WEEK = $matchweek")
CAL_DT RETAIL_WEEK RETAIL_YEAR METRIC_AMT ANOMALY DAY_OF_WEEK
1 1/20/2009 3 2009 2334547 FALSE 3
2 1/19/2010 3 2010 9854269 FALSE 3
3 1/18/2011 3 2011 1951332 FALSE 3
4 1/17/2012 3 2012 8419327 TRUE 3
5 1/15/2013 3 2013 7788004 TRUE 3
6 1/14/2014 3 2014 2130731 TRUE 3
But I want scroll through rows and want to return a list of data frames where each data frame consists of matches for that particular row. For some reason this code does not produce the desired output:
find_dates <- function(file,length){
data <- alignment(file)
matches <- list()
#extract dataset from file and split by aligned dates
for (i in 1:8){
#find matching days with corresponding day_of_week and retail_week
matchday <- data[i,]$DAY_OF_WEEK
matchweek <- data[i,]$RETAIL_WEEK
matches[i] <- fn$sqldf("select * from data where RETAIL_WEEK = $matchday and DAY_OF_WEEK = $matchweek")
}
return(matches)
}
But then I get
[[1]]
character(0)
[[2]]
character(0)
[[3]]
character(0)
[[4]]
[1] "1/4/2009" "1/3/2010" "1/2/2011" "1/1/2012" "12/30/2012" "12/29/2013"
[[5]]
[1] "1/11/2009" "1/10/2010" "1/9/2011" "1/8/2012" "1/6/2013" "1/5/2014"
[[6]]
[1] "1/18/2009" "1/17/2010" "1/16/2011" "1/15/2012" "1/13/2013" "1/12/2014"
[[7]]
[1] "1/25/2009" "1/24/2010" "1/23/2011" "1/22/2012" "1/20/2013" "1/19/2014"
[[8]]
[1] "2/1/2009" "1/31/2010" "1/30/2011" "1/29/2012" "1/27/2013" "1/26/2014"
Warning messages:
1: In matches[i] <- fn$sqldf("select * from data where RETAIL_WEEK = $matchday and DAY_OF_WEEK = $matchweek") :
number of items to replace is not a multiple of replacement length
2: In matches[i] <- fn$sqldf("select * from data where RETAIL_WEEK = $matchday and DAY_OF_WEEK = $matchweek") :
number of items to replace is not a multiple of replacement length
3: In matches[i] <- fn$sqldf("select * from data where RETAIL_WEEK = $matchday and DAY_OF_WEEK = $matchweek") :
number of items to replace is not a multiple of replacement length
4: In matches[i] <- fn$sqldf("select * from data where RETAIL_WEEK = $matchday and DAY_OF_WEEK = $matchweek") :
number of items to replace is not a multiple of replacement length
5: In matches[i] <- fn$sqldf("select * from data where RETAIL_WEEK = $matchday and DAY_OF_WEEK = $matchweek") :
number of items to replace is not a multiple of replacement length
6: In matches[i] <- fn$sqldf("select * from data where RETAIL_WEEK = $matchday and DAY_OF_WEEK = $matchweek") :
number of items to replace is not a multiple of replacement length
7: In matches[i] <- fn$sqldf("select * from data where RETAIL_WEEK = $matchday and DAY_OF_WEEK = $matchweek") :
number of items to replace is not a multiple of replacement length
8: In matches[i] <- fn$sqldf("select * from data where RETAIL_WEEK = $matchday and DAY_OF_WEEK = $matchweek") :
number of items to replace is not a multiple of replacement length
Can someone tell me where I'm going wrong here? I also don't understand why I'm getting the warnings.