-1

I have data of movies and theaters such as:

Theater   , MovieGenre , MovieName , Ticket Sales 
A            Comedy           aa       100
A            Romance          bb        70
A            Action           cc        80
B            Comedy           aa        120
B            Romance          cc        90
B            Romance         dd         50
C            Comedy          aa         87
C            Comedy          ee         86
C            Action          cc         76

I'm trying to build a loop where it gives theater based ticket sales for example:

A 250
B 260
C 249 

I'm using subset:

Select = subset(data, Theater ==A) 

But it is making me put the theater alphabet everytime. How I can automate the process in building a vector.

pnuts
  • 58,317
  • 11
  • 87
  • 139
  • This question is poorly formatted. What is the long string (beginning with "Theater") supposed to be. Is that a single row spanning two lines? Is everything after the third comma a single cell? – Henry David Thorough Feb 24 '14 at 23:34

1 Answers1

1

Try using tapply to apply the needed function within groups, in this case that would be sum:

 > dat <- read.table(text="Theater MovieGenre  MovieName  TicketSales 
+ A            Comedy           aa       100
+ A            Romance          bb        70
+ A            Action           cc        80
+ B            Comedy           aa        120
+ B            Romance          cc        90
+ B            Romance         dd         50
+ C            Comedy          aa         87
+ C            Comedy          ee         86
+ C            Action          cc         76", header=TRUE)
> with(dat, tapply(TicketSales, Theater, sum))
  A   B   C 
250 260 249 
IRTFM
  • 258,963
  • 21
  • 364
  • 487