1

I have a data frame, a reproducible example is:

structure(list(Events = c("a", "a", "a", "b", "b", "b", "c", 
"c", "c", "c"), Locations = c("Loca1", "Loca2", "Loca3", "Loca4", 
"Loca5", "Loca6", "Loca7", "Loca8", "Loca9", "Loca10"), Coll = c(78115.68, 
58361.86, 37409.54, 228922.82, 192700.77, 121201.98, 53941.03, 
53065.56, 46271.9, 211083.11)), .Names = c("Events", "Locations", 
"Coll"), class = "data.frame", row.names = c(NA, 10L))

It looks like this:

   Events Locations      Coll
1       a     Loca1  78115.68
2       a     Loca2  58361.86
3       a     Loca3  37409.54
4       b     Loca4 228922.82
5       b     Loca5 192700.77
6       b     Loca6 121201.98
7       c     Loca7  53941.03
8       c     Loca8  53065.56
9       c     Loca9  46271.90
10      c    Loca10 211083.11

I am trying to create a nested summary table where I want Events as Columns and Locations and Coll Values as Rows. My expected output is:

          a                         b
Locations       Coll          Locations    Coll
Loca1           78115.68       Loca4     228922.82
Loca2           58361.86       Loca5     192700.77
Loca3           37409.54       Loca6     121201.98

I tried using reshape2 recast function...but it fills the table with NA values which I don't want. Any thoughts would be very helpful.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
LeArNr
  • 635
  • 1
  • 6
  • 12
  • 2
    Do you want to [split your dataframe](http://stackoverflow.com/questions/3302356/how-to-split-a-data-frame)? you could try `split(data,data$Events)` – NicE Mar 01 '16 at 13:24
  • Where is the 'c' columns? – akrun Mar 01 '16 at 13:26
  • Exactly what I needed.... :-) Many thanks. – LeArNr Mar 01 '16 at 13:29
  • @akrun i have not included c...just felt a bit lazy... – LeArNr Mar 01 '16 at 13:30
  • @NicE didn't now this existed. Can you make an answer so I can upvote that. Just show the code and the return of what you expect to show and it will cover what is necessary for a good answer. – Cayce K Mar 01 '16 at 13:40
  • Probably a package like tables is the best way to go https://cran.r-project.org/web/packages/tables/index.html – Frank Apr 04 '17 at 16:58

0 Answers0