IDG Deep Dive
part 1 1 IT World
IT World 2
setwd( ~/mydirectory ) setwd( C:/Sharon/Documents/RProjects ) install.packages( thepackagename ) installed.packages() library( thepackagename ) 3 IT World
update.packages() remove.packages( thepackagename )?functionname help(functionname) example(functionname) help.search( your search term )??( my search term ) IT World 4
part 2 data() mtcars 5 IT World
mydata <- read.csv( filename.txt ) mydata <- read.csv( filename.txt, header=false) mydata <- read.table( filename.txt, sep= \t, header=true) mydata <- read.table( filename.txt, sep= \t, header=true,stringsasfactor=false) IT World 6
x <- read.table(file = clipboard, sep= \t, header=true) x <- read.table(pipe( pbpaste ), sep= \t ) 7 IT World
mydata <- read.csv( http://bit.ly/10er84j ) pew_data <- read.csv( http://bit.ly/11i3iuu ) IT World 8
install.packages( quantmod ) library( quantmod ) getsymbols( AAPL ) barchart(aapl) barchart(aapl, subset= last 14 days ) chartseries(aapl, subset= last 14 days ) barchart(aapl[ 2013-04-01::2013-04-12 ]) rm(x) 9 IT World
save.image() save(variablename, file= filename.rda ) load( filename.rda ) IDG Tech Library IT World 10
part 3 head(mydata) head(mydata, n=10) head(mydata, 10) tail(mydata) tail(mydata, 10) 11 IT World
str(mydata) colnames(mydata) rownames(mydata) summary(mydata) IT World 12
install.packages( psych ) library(psych) describe(mydata) cor(mydata) na.rm=true 13 IT World
mean(myvector, na.rm=true)?median choose(15,4) mypeople <- c( Bob, Joanne, Sally, Tim, Neal ) combn(mypeople, 2) combn(c( Bob, Joanne, Sally, Tim, Neal ),2) IT World 14
mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb. names(mtcars) mtcars.colnames <- names(mtcars) mtcars$mpg dataframename$columnname [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 [12] 16.4 17.3 15.2 10.4 10.4 14.7 32.4 30.4 33.9 21.5 15.5 [23] 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 15.0 21.4 15 IT World
mtcars[,2:4] mtcars[,c(2,4)] mtcars[,c(2,4)] mtcars$mpg>20 IT World 16
[1] TRUE TRUE TRUE TRUE FALSE FALSE FALSE TRUE TRUE [10] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE [19] TRUE TRUE TRUE FALSE FALSE FALSE FALSE TRUE TRUE [28] TRUE FALSE FALSE FALSE TRUE mtcars[mtcars$mpg>20,] mtcars[mtcars$mpg>20,c(1,4)] mtcars[mtcars$mpg>20,c( mpg, hp )] 17 IT World
mpg20 <- mtcars$mpg > 20 cols <- c( mpg, hp ) mtcars[mpg20, cols] attach(mtcars) mpg20 <- mtcars$mpg > 20 mpg20 <- mpg > 20 detach() subset subset(mtcars, mpg>20, c( mpg, hp )) IT World 18
subset(mtcars, mpg==max(mpg)) subset(mtcars, mpg==max(mpg), mpg) subset(mtcars,, c( mpg, hp )) subset(mtcars, select=c( mpg, hp )) table(diamonds$cut) table(diamonds$cut, diamonds$color) 19 IT World
part 4 plot(mtcars$disp, mtcars$mpg) plot(mtcars$disp, mtcars$mpg, xlab= Engine displacement, ylab= mpg, main= MPG compared with engine displacement ) plot(mtcars$disp, mtcars$mpg, xlab= Engine displacement, ylab= mpg, main= MPG vs engine displacement, las=1)?par IT World 20
install.packages( ggplot2 ) library(ggplot2) qplot(disp, mpg, data=mtcars) qplot(disp, mpg, ylim=c(0,35), data=mtcars) 21 IT World
qplot(cty, hwy, data=mpg) qplot(cty, hwy, data=mpg, geom= jitter ) ggplot(mtcars, aes(x=disp, y=mpg)) + geom_point() ggplot(pressure, aes(x=temperature, y=pressure)) + geom_line() IT World 22
ggplot(mydata, aes(x=xcol, y=ycol), ylim=0) + geom_line() ggplot(pressure, aes(x=temperature, y=pressure)) + geom_line() + geom_point() barplot(bod$demand) barplot(bod$demand, main= Graph of demand ) barplot(bod$demand, main= Graph of demand, names.arg = BOD$Time) 23 IT World
cylcount <- table(mtcars$cyl) 4 6 8 11 7 14 barplot(cylcount) qplot(mtcars$cyl) qplot(factor(mtcars$cyl)) ggplot(mtcars, aes(factor(cyl))) + geom_bar() hist(mydata$columnname, breaks = n) IT World 24
qplot(columnname, data=mydata, binwidth=n) ggplot(mydata, aes(x=columnname)) + geom_histogram(binwidth=n) boxplot(mtcars$mpg) boxplot(diamonds$x, diamonds$y, diamonds$z) 25 IT World
rainbow(n) heat.colors(n) terrain.colors(n) topo.colors(n) cm.colors(n) rainbow(5)?rainbow mycolors <- rainbow(3) mycolors <- heat.colors(3) ggplot(mtcars, aes(x=factor(cyl))) + geom_bar(fill=mycolors) ggplot(mtcars, aes(x=factor(cyl))) + geom_bar(fill=rainbow(3)) IT World 26
barplot(bod$demand, col=rainbow(6)) barplot(bod$demand, col= royalblue3 ) testscores <- c(96, 71, 85, 92, 82, 78, 72, 81, 68, 61, 78, 86, 90) barplot(testscores) barplot(testscores, col= blue ) testcolors <- ifelse(testscores >= 80, blue, red ) barplot(testscores, col=testcolors) 27 IT World
barplot(testscores, col=testcolors, main= Test scores ) barplot(testscores, col=testcolors, main= Test scores, ylim=c(0,100)) barplot(testscores, col=testcolors, main= Test scores, ylim=c(0,100), las=1) testscores <- sort(c(96, 71, 85, 92, 82, 78, 72, 81, 68, 61, 78, 86, 90), decreasing = TRUE) testscores <- c(96, 71, 85, 92, 82, 78, 72, 81, 68, 61, 78, 86, 90) testscores_sorted <- sort(testscores, decreasing = TRUE) ggplot(results, aes(x=students, y=testscores)) + geom_bar(fill=testcolors, stat = identity ) IT World 28
qplot(factor(cyl), data=mtcars, geom= bar, fill=factor(cyl)) jpeg( myplot.jpg, width=350, height=420) barplot(bod$demand, col=rainbow(6)) dev.off() 29 IT World
part 5 x <- 3 x = 3 myarray = array(1, 1, 2, 3, 5, 8); IT World 30
int myarray = {1, 1, 2, 3, 5, 8}; myarray = [1, 1, 2, 3, 5, 8] my_vector <- c(1, 1, 2, 3, 5, 8) my_vector <- (1:10) my_vector <- c(1:10) my_vector <- c(1, 4, hello, TRUE) 31 IT World
My_list <- list(1,4, hello, TRUE) my_vector <- c(7,9,23,5) my_pct_vector <- my_vector * 0.01 apply(my_matrix, 1, median) apply(my_matrix, 2, median) IT World 32
1, 5, 7 Bill, Bob, Sue 33 IT World
[1] class(3) class(3.0) class(3l) class(as.integer(3)) dim(my_matrix) IT World 34
35 IT World
sqldf( select * from mtcars where mpg > 20 order by mpg desc ) edit(mtcars) write.table(mydata, testfile.txt, sep= \t ) IT World 36
part 6 37 IT World
IT World 38
39 IT World
IT World 40
41 IT World
IT World 42
43 IT World
IT World 44