Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, you should be storing your rules in a list, rather than a data.frame. This saves your from having to pad each "rule list" with NAs to make them the same length, and also allows you to process your data with <code>lapply</code>.</p> <p>Since you didn't provide any data, I made some up:</p> <pre><code>#Load data set.seed(42) library(quantmod) getSymbols('SPY') SPY &lt;- adjustOHLC(SPY) dat &lt;- dailyReturn(Cl(SPY)) #Add some TTRs for (rule in c('RSI', 'SMA')){ for (n in c(5, 10, 15, 20, 25)){ newvar &lt;- paste(rule, n, sep='_') FUN &lt;- get(rule) dat &lt;- cbind(dat, FUN(dat[,1], n=n)) names(dat)[length(names(dat))] &lt;- newvar } } dat &lt;- na.omit(dat) rulenames &lt;- names(dat)[-1] </code></pre> <p>Note that this is an <code>xts</code> object, not a data.frame. This is important, as it keeps the index in the <code>Date</code> format, rather than as a character vector:</p> <pre><code>&gt; dat[1:5, 1:5] daily.returns RSI_5 RSI_10 RSI_15 RSI_20 2007-02-08 -0.001308450 40.06379 46.99824 48.59484 49.11738 2007-02-09 -0.007447249 26.65296 40.34267 44.35689 46.10753 2007-02-12 -0.003404196 42.49883 45.94447 47.58264 48.30373 2007-02-13 0.008434995 67.89045 58.59450 55.64932 54.07276 2007-02-14 0.006567123 62.45177 56.28547 54.23836 53.08886 </code></pre> <p>I also made up some TTRs to use for each year</p> <pre><code>#Make a list of rules for each year library(lubridate) dat$Year &lt;- year(index(dat)) uniqueYear &lt;- sort(unique(dat$Year)) rulesList &lt;- lapply(uniqueYear, function(x) rulenames[runif(length(rulenames))&gt;.5]) names(rulesList) &lt;- uniqueYear </code></pre> <p>Note that my ruleList is literally a list:</p> <pre><code>&gt; rulesList $`2007` [1] "RSI_5" "RSI_10" "RSI_20" "RSI_25" "SMA_5" "SMA_10" "SMA_20" "SMA_25" $`2008` [1] "RSI_10" "RSI_15" "SMA_5" "SMA_10" "SMA_25" $`2009` [1] "RSI_5" "RSI_15" "RSI_20" "SMA_5" "SMA_15" "SMA_25" $`2010` [1] "RSI_5" "RSI_10" "RSI_20" "SMA_5" "SMA_20" "SMA_25" $`2011` [1] "RSI_20" "SMA_5" "SMA_10" "SMA_15" "SMA_20" "SMA_25" $`2012` [1] "RSI_20" "SMA_5" "SMA_10" "SMA_25" </code></pre> <p>Now it's simply a matter of looping through each year, and subsetting the <code>dat</code> object to the proper rows (year) and columns (TTRs):</p> <pre><code>#Apply the rules to each data.frame data.by.year &lt;- lapply(uniqueYear, function(year){ rule_subset &lt;- rulesList[[as.character(year)]] data_subset &lt;- dat[dat$Year==year, rule_subset] }) names(data.by.year) &lt;- uniqueYear </code></pre> <p>The <code>data.by.year</code> is a list (of length 6), where each element represent 1 year's worth of data, with the selected TTRs.</p> <pre><code>&gt; str(data.by.year[[1]]) An ‘xts’ object from 2007-02-08 to 2007-12-31 containing: Data: num [1:226, 1:8] 40.1 26.7 42.5 67.9 62.5 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:8] "RSI_5" "RSI_10" "RSI_20" "RSI_25" ... Indexed by objects of class: [Date] TZ: xts Attributes: List of 3 $ tclass : chr "Date" $ tzone : chr "" $ na.action:Class 'omit' atomic [1:25] 1 2 3 4 5 6 7 8 9 10 ... .. ..- attr(*, "index")= num [1:25] 1.17e+09 1.17e+09 1.17e+09 1.17e+09 1.17e+09 ... &gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload