Skip to content
Prev 301353 / 398506 Next

Passing arguments to SQL Query in R

Let me count the ways...

R supplies a number of different ways.  Here is sample using basic R and some other packages.  Youprobably will  need to install the packages ( ?install.packages) to run any but aggregate(). 

mydata <- structure(list(Name = structure(c(4L, 4L, 4L, 4L, 2L, 2L, 2L, 
3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L), .Label = c("Jacques K", 
"Rahul D  ", "Ricky P  ", "Sachin T "), class = "factor"), Score = c(25L, 
53L, 57L, 34L, 38L, 31L, 53L, 7L, 45L, 27L, 17L, 86L, 48L, 23L, 
86L, 32L)), .Names = c("Name", "Score"), class = "data.frame", row.names = c(NA, 
-16L))

# R basic
aggregate(Score ~ Name, mean, data = mydata)

library(reshape2) 
dcast(mydata, Name ~ . , mean)

library(plyr)
ddply(mydata, .(Name), summarize, Mean = mean(Score))

library(data.table)
DT  <-  data.table(mydata)
DT[, mean(Score),by=Name]

John Kane
Kingston ON Canada
____________________________________________________________
FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!