title: Partition several vectors into groups (knowing the range of the values of each group) for barplots
body: <p>My name is Natasa, I’m new in R. I’m impressed by what R can do, but unfortunately I don’t have the time to learn it from the beginning.</p>
  <p>I have a lot of vectors (11) with 10000 values/numbers each, so I will be using a more “compact” version. Let’s say that I have 4 vectors: Where TI=Time, and RE= Region (1, 2 or 3).</p>
  <pre><code>TI -&gt; c(10, 20, 30, 40, 50, 100, 150, 200, 300)
  RE1 -&gt; c(0.25, 0.78, 0.35, 0.37, 4.56, 5.23, 3.75, 8.51, 10.85)
  RE2 -&gt; c(0.05, 1.54, 0.4, 0.42, 2.53, 1.38, 4.58, 10.54, 25.35)
  RE3 -&gt; c(0.02, 0.53, 0.72, 0.28, 7.82, 13.51, 23.54, 2.15)
  </code></pre>
  <p>I want to create groups of “TI” (Time series: group1= TI corresponding to 10, 20, 30 and 40, group2= between 50-150 and group3= 200 and 300) and compute the mean and stdev for each RE vector according to /depending on the groups of TI. Each group is of unequal length and I don’t know the number of “variables” in each group (only the “range”). My final goal is to create a grouped bar plot for each group of TI and for each RE vector. In x axis there will be the groups of TI (the time series) and in y axis “values” of the regions, where in each time series there will be a separate “histogram” for each region.</p>
  <p>I have found on the internet several pages and I have tried several things, but without any success.
  My thoughts were:</p>
  <ol>
  <li>To create a “table” (using the cbind function) like this:
  All -&gt; cbind(TI, RE1, RE2, RE3)</li>
  <li>Partition the TI vector into groups and the other vectors according to the TI grouping. The pages that I have found are:
  Using the split function, as in:
  <a href="https://stackoverflow.com/questions/11737193/replace-given-value-in-vector">How to partition a vector into groups of neighbors in R?</a>
  <a href="https://stackoverflow.com/questions/5222061/create-grouping-variable-for-consecutive-sequences-and-split-vector">Split a vector into three vectors of unequal length in R</a>
  or
  rename all the different values of TI according to the groups (group1, group2 and group3) using the replace function, like in:
  <a href="https://stackoverflow.com/questions/11737193/replace-given-value-in-vector">Replace given value in vector</a></li>
  <li>Use the aggregate function like in:
  <a href="https://stackoverflow.com/questions/21982987/mean-per-group-in-a-data-frame">Mean per group in a data.frame</a>
  or
  <a href="https://stackoverflow.com/questions/9378482/r-how-can-i-create-a-table-with-mean-and-sd-according-to-experimental-group-alo">R: how can I create a table with mean and sd according to experimental group alongside p-values?</a></li>
  <li>And finally use the barplot function.</li>
  </ol>
  <p>The only problem is that I can’t found the correct way to split the table in the desired groups or in an “easy” way to rename specific values of TI (thought 2).
  Wanted table (If my &quot;thoughts&quot; are correct)</p>
  <pre><code>TI RE1 RE2 RE3
  group1 0.25 0.05 0.02
  group1 0.78 1.54 0.53
  group1 0.35 0.4 0.72
  group1 0.37 0.42 0.28
  group2 4.56 2.53 7.82
  group2 5.23 1.38 13.51
  group2 3.75 4.58 23.54
  group3 8.51 10.54 2.15
  group3 10.85 25.35 0.65
  </code></pre>
  <p>Since my data is large, I don’t think that the replace function for each value is “affordable”. My other thought was to compute separately the mean and SD for each group of TI and RE and then to insert a column with the desire names of the group and then combine all the “tables” in one… but it will be very time consuming and not practical. Is there a way to “say” in R to rename all the numbers between 10-40 to group1, values between 50-150 to group2 etc. of the vector TI or that the numbers between… are a group etc.? If not, is there an easiest way to compute mean and sd for a specific range of values of a different vector? Or all those things aren’t needed and I can do it using the barplot function (I also tried to do it… without any success)?</p>
  <p>It is really hard for me to figure it out with such limited experience, and any help will be greatly appreciated!! Thanks in advance for your responses.</p>