0

I have a data frame that looks like:

                uri month
1 /product/product2   Jun
2 /product/product3   Jun
3 /product/product3   Jun
4      /feeds/press   Jun
5 /product/product3   Jun
6 /product/product3   Jun

I want to create another data frame that looks like

uri                       Jan          Feb        Mar             etc.
/product/product2         1938         5785       4842
/feeds/press              523894       34829      398423

These numbers are all examples (not the real totals)

I was able to create something like this using:

#Reorder Months in Calendar Year for /demo URI
demo_month = as.matrix(
as.matrix(summary(uri_month[uri_month$uri == "/demo", "month" ]))
[c(5,4,8,1,9,7,6,2,12,11,10,3),])

such that demo_month looks like

     [,1]
Jan 12845
Feb 11716
Mar 11627
Apr 11005
May 12362
Jun 12360
Jul 12688
Aug 11526
Sep 11105
Oct  2544
Nov 17056
Dec 14137

Essentially, I would want to repeat the code I used for the /demo uri for each level. I know I can do it manually and just replace "/demo", then combine it, but I have 130 different types of URI. I'm a beginner in R, and would prefer to not use any packages. I believe I would need to use tapply() somehow.

Thanks!

camelarms
  • 100
  • 1
  • 8
  • It may seem to me that you want to reshape the data. Although, you may not prefer to use any package, it would be easy to solve using reshape2 package. [Here](http://stackoverflow.com/questions/15668870/reshape-wide-format-to-multi-column-long-format/15669045#15669045) is the example. – Metrics Jun 25 '13 at 22:24

1 Answers1

0

I'm not sure if I completely understand your question, but I believe the following code will reorder the months in the year for each row in your matrix, then save them as columns into a new matrix. I hope this helps:

m<-matrix(0,nrow=12,ncol=nrow(uri_month))
for(i in 1:nrow(uri_month)){
  m[,i] = as.matrix(as.matrix(summary(uri_month[uri_month[i,], "month" ]))
  [c(5,4,8,1,9,7,6,2,12,11,10,3),])
}
Ryan
  • 121
  • 1
  • 1
  • 11
  • I decided to approach my issue another way (sorting by month, instead of uri). Thank you though, I needed to use your code for another issue I had. :) – camelarms Jun 26 '13 at 16:28