0

The data frame I use:

head(tips)
  obs totbill  tip sex smoker day  time size
1   1   16.99 1.01   F     No Sun Night    2
2   2   10.34 1.66   M     No Sun Night    3
3   3   21.01 3.50   M     No Sun Night    3
4   4   23.68 3.31   M     No Sun Night    2
5   5   24.59 3.61   F     No Sun Night    4
6   6   25.29 4.71   M     No Sun Night    4

My assignment asks me to compute the average tip, total tip and average size grouped by smoker and day. I use tapply function to compute them:

 avetip_d <- tapply(tips$tip,tips$day,mean)
 avetip_s <- tapply(tips$tip,tips$smoker,mean)
 avebill_d <- tapply(tips$totbill,tips$day,mean)
 avebill_sm <- tapply(tips$totbill,tips$smoker,mean)
 avesize_d <- tapply(tips$size,tips$day,mean)
 avesize_sm <- tapply(tips$size,tips$smoker,mean)

The outputs are :

tapply(tips$tip,tips$day,mean)
 Fri      Sat      Sun      Thu 
 2.734737 2.993103 3.255132 2.771452 
tapply(tips$tip,tips$smoker,mean)
  No      Yes 
  2.991854 3.008710 
tapply(tips$totbill,tips$day,mean)
 Fri      Sat      Sun      Thu 
17.15158 20.44138 21.41000 17.68274 
tapply(tips$totbill,tips$smoker,mean)
  No      Yes 
 19.18828 20.75634 

The data frame I use is called tips. How can I report them in a nice table? For each combination of smoker and day you should have a row of these summaries. And, is there any easier way to compute those means?

T.T
  • 27
  • 1
  • 4
  • 1
    Please provide a small reproducible example and expected output based on that – akrun Sep 29 '17 at 17:23
  • I meant to show a small input dataset for others to work on by editing on your post – akrun Sep 29 '17 at 17:28
  • I'd recommend taking a look at this question: https://stackoverflow.com/questions/21982987/mean-per-group-in-a-data-frame you may need to set up your data slightly differently, but it appears that it'd be minimal effort. – Badger Sep 29 '17 at 17:29

0 Answers0