title: split up xts-dataframe into several groups, collapse to weekly data and keep the time index
body: <p>I am a total newbie to R so I apologize if the answer to my question is too obvious.  I a data set of the following form:</p>
  
  <pre><code>Date, V1,V...,VN,Land,Nace
  22/03/1995 23:01:12,1,3,2,15,A
  21/03/1995 21:01:12,3,3,1,9,C
  1/04/1995 17:01:06,3,2,1,3,B   
  </code></pre>
  
  <p>I would like to analyze the data in the data.frame by Land, NACE (it's an industry code), Date (I would like to collapse the whole think to weekly data) and by the three different answering options {1,2,3} in V1...VN. This is a sample of my data: </p>
  
  <pre><code>example &lt;- as.data.frame(structure(c(" 1", " 2", " 1", " 2", " 1", " 1", " 2", " 1", " 2", 
  " 1", " 2", " 3", " 1", " 1", " 2", " 2", " 3", " 1", " 2", " 2", 
  " 1", " 2", " 1", " 1", " 2", NA, " 2", NA, NA, " 1", " 3", " 1", 
  " 3", " 3", " 2", " 3", " 3", " 3", " 2", " 2", " 2", " 3", " 3", 
  " 3", " 2", " 2", " 3", " 3", " 3", " 3", " 1", " 2", " 1", " 2", 
  " 2", " 1", " 2", " 1", " 2", " 2", " 2", " 3", " 1", " 1", " 2", 
  " 2", " 3", " 3", " 2", " 2", " 1", " 2", " 1", " 1", " 2", NA, 
  " 2", NA, NA, " 1", " 3", " 2", " 3", " 2", " 0", " 3", " 3", 
  " 3", " 2", " 0", " 2", " 3", " 3", " 3", " 0", " 2", " 2", " 3", 
  " 3", " 0", "12", " 5", " 9", "14", " 5", "tra", "tra", "man", 
  "inf", "agc", "07-2011", "07-2011", "07-2011", "07-2011", "07-2011" 
  ), .indexCLASS = c("POSIXlt", "POSIXt"), .indexTZ = "", class = c("xts", 
  "zoo"), .indexFORMAT = "%U-%Y", index = structure(c(1297642226, 
  1297672737, 1297741204, 1297748893, 1297749513), tzone = "", tclass = c("POSIXlt", 
  "POSIXt")), .Dim = c(5L, 23L), .Dimnames = list(NULL, c("rev_sit", 
  "prof_sit", "emp_nr_sit", "inv_sit", "ord_home_sit", "ord_abr_sit", 
  "emp_cost_sit", "usage_cost_sit", "tax_cost_sit", "gov_cost_sit", 
  "rev_exp", "prof_exp", "emp_nr_exp", "inv_exp", "ord_home_exp", 
  "ord_abr_exp", "emp_cost_exp", "usage_cost_exp", "tax_cost_exp", 
  "gov_cost_exp", "land", "nace", "index")))) 
  </code></pre>
  
  <p>prof_sit etc. are questions and below there are the answer on the scale 1,2,3. land, nace and index (that's the time index) are the variables with the help of which i would like to split up the dataset. The goal is to get an xts data frame which would look like:</p>
  
  <pre><code>-,nace.land,nace.land,nace.land,...
  10-1995,sum of answers coded i.e. as 1 for a certain nace and a certain land,sum,sum,...  
  11-1995,sum,sum,...
  12-1995,sum,sum,...
  </code></pre>
  
  <p>where 12-1995 is the 12th calendar week in 1995. The nearest I came to this solution was with tapply:</p>
  
  <pre><code>pos &lt;- as.data.frame(tapply((example[,1]==3)*1,
    list(example$index, example$land, example$nace), sum)) 
  </code></pre>
  
  <p>It does more or less what I want with the downturn that the format as an xts object is lost and thus that the rows are not in the right order. as a second disadavantage it might also be mentioned that i will to run loops to apply the same technique for all the twenty questions. does anybody know a solution for that problem? i appreciate every help or hint since i am wasting my time on this problem since several days now.</p>
  
  <p>best regards,</p>
  
  <p>andreas </p>