-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecipe2.R
More file actions
30 lines (22 loc) · 1.12 KB
/
Recipe2.R
File metadata and controls
30 lines (22 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
## ------------------------------------------------------------------------
#install.packages('plyr')
#install.packages('xtable')
## ------------------------------------------------------------------------
library("plyr")
## ------------------------------------------------------------------------
data(mtcars)
#summarize MPG data in mtcars by number of cylinders
ddply(mtcars,.(cyl),summarize,avg_MPG=mean(mpg))
#summarize MPG data in mtcars by transmission
ddply(mtcars,.(am),summarize,avg_MPG=mean(mpg))
# Let's modify am into a factor with labels 0="AUTOMATIC",1="MANUAL"
mtcars$f_am<-factor(mtcars$am,levels=c(0,1),labels=c("AUTOMATIC","MANUAL"))
# Now, let's summarize it again by the new factor
ddply(mtcars,.(f_am),summarize,avg_MPG=mean(mpg))
#summarize MPG and weight data in mtcars by number of cylinders and transmission
my_table<-ddply(mtcars,.(cyl,f_am),summarize,avg_MPG=mean(mpg),avg_WT=mean(wt))
print(my_table)
## ----results='asis'------------------------------------------------------
library(xtable)
xtab<-xtable(my_table,caption="Average Consumption per number of cylinders", label="lab:avgmpg")
print(xtab,type="latex")