Download presentation
Presentation is loading. Please wait.
Published bySherman Booth Modified over 5 years ago
1
R in Action Obtaining and installing R and RStudio
Obtaining and installing R and RStudio R is freely available from the Comprehensive R Archive Network (CRAN) at Y R studio: #Working with R set.seed(34) x <- rnorm(5) Comments are preceded by the # symbol set.seed: Set the seed of R's random number generator, which is useful for creating random objects that can be reproduced Creates a vector object named x containing five random deviates from a standard normal distribución
2
Working with R #The function c()combines arguments into a vector age <- c(1,3,5,2,11,9,3,9,12,3) weight <- c(4.4,5.3,7.2,5.2,8.5,7.3,6.0,10.4,10.2,6.1) mean(weight) #arithmetic mean sd(weight) #standard deviation cor(age,weight) #correlation of x and y plot(age,weight) #plot of x and y #Randon uniform distribution x <- runif(20) summary(x) #summary hist(x) # histogram #Help help.start() #General help data() #List all available example datasets
3
Installing a package (with internet)
Packages Packages are collections of R functions, data, and compiled code in a well-defined format #Packages library() #shows you what packages you’ve saved in your library search() #tells you which packages are loaded and ready to use R comes with a standard set of packages (including base, datasets, utils, grDevices, graphics, stats, and methods). Installing a package (with internet) #Installing a package install.packages("mlbench") # install the package mlbench Load a package #Load a package library(mlbench) # load and attach the package mlbench
4
Using output as input—reusing results
Mtcars # 1974 Motor Trend US magazine head(mtcars) # Returns the first parts of a vector, matrix, table, or data frame fit <- lm(mpg~wt, data=mtcars) # lm(): fit linear models, regression summary(fit) plot(mtcars$wt,mtcars$mpg) title("Regression of MPG on Weight") abline(lmfit) # Add Straight Lines to a Plot
5
Vectors Vectors are one-dimensional arrays that can hold numeric data, character data, or logical data. The combine function c() is used to form the vector #Vectors a <- c(1, 2, 5, 3, 6, -2, 4) b <- c("one", "two", "three") c <- c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE) The data in a vector must only be one type or mode (numeric, character, or logical). You can’t mix modes in the same vector You can refer to elements of a vector using a numeric vector of positions within brackets # Refer to elements of a vector a <- c(1, 2, 5, 3, 6, -2, 4) a[3] a[c(1, 3, 5)] a[2:6]
6
y <- matrix(1:20, nrow=5, ncol=4) y
Matrices A matrix is a two-dimensional array where each element has the same mode (numeric, character, or logical) # Matrices y <- matrix(1:20, nrow=5, ncol=4) y You can identify elements of a matrix X[i,] refers to the ith row X[,j] refers to jth column X[i, j] refers to the ijth element # identify elements x <- matrix(1:10, nrow=2) X x[2,] x[,2] x[1,4] x[1, c(4,5)]
7
Data frames A data frame is more general than a matrix in that different columns can contain different modes of data (numeric, character, etc.) #Data frames patientID <- c(1, 2, 3, 4) age <- c(25, 34, 28, 52) diabetes <- c("Type1", "Type2", "Type1", "Type1") status <- c("Poor", "Improved", "Excellent", "Poor") patientdata <- data.frame(patientID, age, diabetes, status) patientdata Specifying elements of a data frame # Specifying elements patientdata[1:2] patientdata[c(3,4)] patientdata$age # Contingency table of the counts at each combination of factor levels table(patientdata$diabetes, patientdata$status)
8
Attach, Detach, and With #Attach attach(mtcars) summary(mpg) plot(mpg, disp) detach(mtcars) #With with(mtcars, { summary(disp) plot(mpg, wt) })
9
Using factors #Factors patientID <- c(1, 2, 3, 4) age <- c(25, 34, 28, 52) diabetes <- c("Type1", "Type2", "Type1", "Type1") status <- c("Poor", "Improved", "Excellent", "Poor") diabetes <- factor(diabetes) status <- factor(status, order=TRUE) patientdata <- data.frame(patientID, age, diabetes, status) str(patientdata) summary(patientdata)
10
Lists #List g <- "My First List" h <- c(25, 26, 18, 39) j <- matrix(1:10, nrow=5) k <- c("one", "two", "three") mylist <- list(title=g, ages=h, j, k) Mylist mylist[[2]] mylist[["ages"]]
11
Entering data from the keyboard
The edit() function invoke a text editor # Text editor mydata <- data.frame(age=numeric(0), gender=character(0), weight=numeric(0)) mydata <- edit(mydata) Entering data from Excel # Data from clipboard gastos<-read.table("clipboard",header=TRUE) # Data from csv file Gastos<-read.csv(“Gastos.cvs",head=T)
12
Working with graphs # Graph example dose <- c(20, 30, 40, 45, 60) drugA <- c(16, 20, 27, 40, 60) drugB <- c(15, 18, 25, 31, 40) plot(dose, drugA, type="b") title("Drug A") Use help(plot) to view other options
13
opar <- par(no.readonly=TRUE) #opar: current graphical settings
Graphical parameters # Graphical settings par() #list of the current graphical settings par(no.readonly=TRUE) #list of current graphical settings that can be modified # Graphical settings example opar <- par(no.readonly=TRUE) #opar: current graphical settings par(lty=2, pch=17) plot(dose, drugA, type="b") # or: plot(dose, drugA, type="b", lty=2, pch=17) par(opar) #reset graphical settings
14
Parameters for specifying symbols and lines
pch Specifies the symbol to use when plotting points cex Specifies the symbol size. 1=default, 1.5 is 50% larger, 0.5 is 50% smaller lty Specifies the line type lwd Specifies the line width. lwd=2 generates a line twice as wide as the default # symbols and lines plot(dose, drugA, type="b", lty=3, lwd=3, pch=15, cex=2)
15
Parameters for specifying color
col Default plotting color. col=“red” col.axis Color for axis text col.lab Color for axis labels col.main Color for titles col.sub Color for subtitles fg The plot’s foreground color bg The plot’s background color
16
Functions that can be used to create vectors of contiguous colors
rainbow(), heat.colors(), terrain.colors(), topo.colors(), and cm.colors() # contiguous rainbow colors n <- 10 mycolors <- rainbow(n) pie(rep(1, n), labels=mycolors, col=mycolors) Parameters specifying text size cex plotted text. 1=default, 1.5 is 50% larger, 0.5 is 50% smaller cex.axis Magnification of axis text relative to cex cex.lab Magnification of axis labels relative to cex cex.main Magnification of titles relative to cex cex.sub Magnification of subtitles relative to cex par(font.lab=3, cex.lab=1.5, font.main=4, cex.main=2)
17
Parameters specifying font family, size, and style
font Integer specifying font to use for plotted text.. 1=plain, 2=bold, 3=italic, 4=bold italic font.axis Font for axis text font.lab Font for axis labels font.main Font for titles font.sub Font for subtitles family Font family for drawing text. Standard values are serif, sans, and mono. Parameters for graph and margin dimensions pin Plot dimensions (width, height) in inches mai Numerical vector indicating margin size where c(bottom, left, top, right) is expressed in inches mar margin size where c(bottom, left, top, right) is expressed in lines. The default is c(5, 4, 4, 2) par(pin=c(4,3), mai=c(1,.5, 1, .2))
18
Using graphical parameters to control graph appearance
#Two plots dose <- c(20, 30, 40, 45, 60) drugA <- c(16, 20, 27, 40, 60) drugB <- c(15, 18, 25, 31, 40) opar <- par(no.readonly=TRUE) par(pin=c(2, 3)) par(lwd=2, cex=1.5) par(cex.axis=.75, font.axis=3) plot(dose, drugA, type="b", pch=19, lty=2, col="red") plot(dose, drugB, type="b", pch=23, lty=6, col="blue", bg="green") #Two graphs in same plot lines(dose, drugB, type="b", pch=23, lty=6, col="blue", bg="green") par(opar)
19
Adding text, customized axes, and legends
plot(dose, drugA, type="b", col="red", lty=2, pch=2, lwd=2, main="Clinical Trials for Drug A", sub="This is hypothetical data", xlab="Dosage", ylab="Drug Response", xlim=c(0, 60), ylim=c(0, 70)) Axis options Side (1=bottom, 2=left, 3=top, 4=right) at A numeric vector indicating where tick marks should be drawn labels A character vector of labels to be placed at the tick marks pos The coordinate at which the axis line is to be drawn lty Line type. col The line and tick mark color. las Labels are parallel (=0) or perpendicular (=2) to the axis.
20
#An example of custom axes
x <- c(1:10) y <- x z <- 10/x opar <- par(no.readonly=TRUE) par(mar=c(4, 4, 2, 6) + 0.1) plot(x, y, type="b", pch=21, col="red", yaxt="n", lty=3, ann=FALSE) lines(x, z, type="b", pch=22, col="blue", lty=2) axis(2, at=x, labels=x, col.axis="red", las=2) axis(4, at=z, labels=round(z, digits=2), col.axis="blue", las=2, cex.axis=0.7, tck=-.01) mtext("y=1/x", side=4, line=3, cex.lab=1, las=2, col="blue") title("An Example of Creative Axes", xlab="X values", ylab="Y=X") par(opar)
21
Reference lines The abline() function is used to add reference lines to our graph. The format is abline(h=yvalues, v=xvalues) #Add reference lines abline(h=c(1,5,7)) abline(v=c(1, 3, 5, 7, 9), lty=2, col="blue") Legend legend(location, title, legend, ...) location You can give an x, y coordinate for the upper-left corner of the legend. You can use locator(1), in which case you use the mouse to indicate the location of the legend. You can also use the keywords bottom, bottomleft, left, topleft, top, topright, right, bottomright, or center to place the legend in the graph. title A character string for the legend title (optional). legend A character vector with the labels.
22
Comparing Drug A and Drug B response by dose
dose <- c(20, 30, 40, 45, 60) drugA <- c(16, 20, 27, 40, 60) drugB <- c(15, 18, 25, 31, 40) opar <- par(no.readonly=TRUE) par(lwd=2, cex=1.5, font.lab=2) plot(dose, drugA, type="b", pch=15, lty=1, col="red", ylim=c(0, 60), main="Drug A vs. Drug B", xlab="Drug Dosage", ylab="Drug Response") lines(dose, drugB, type="b", pch=17, lty=2, col="blue") abline(h=c(30), lwd=1.5, lty=2, col="gray") library(Hmisc) minor.tick(nx=3, ny=3, tick.ratio=0.5) legend("topleft", inset=.05, title="Drug Type", c("A","B"), lty=c(1, 2), pch=c(15, 17), col=c("red", "blue")) par(opar)
23
text(location, "text to place", pos, ...)
Text annotations text() places text within the graph text(location, "text to place", pos, ...) mtext() places text in one of the four margins mtext("text to place", side, line=n, ...) location Location can be an (x, y) coordinate, or via mouse by specifying location as locator(1) pos Position relative to location. 1 = below, 2 = left, 3 = above, 4 = right. side Which margin to place text in, where 1 = bottom, 2 = left, 3 = top, 4 = right You can also specify adj=0 for left/bottom alignment or adj=1 for top/right alignment.
24
Example of a scatter plot (car weight vs
Example of a scatter plot (car weight vs. mileage) with labeled points (car make) # scatter plot with labeled points attach(mtcars) plot(wt, mpg, main="Mileage vs. Car Weight", xlab="Weight", ylab="Mileage", pch=18, col="blue") text(wt, mpg, row.names(mtcars), cex=0.6, pos=4, col="red") detach(mtcars) #Display font families opar <- par(no.readonly=TRUE) par(cex=1.5) plot(1:7,1:7,type="n") text(3,3,"Example of default text") text(4,4,family="mono","Example of mono-spaced text") text(5,5,family="serif","Example of serif text") par(opar)
25
opar <- par(no.readonly=TRUE) par(mfrow=c(2,2))
Combining graphs R makes it easy to combine several graphs into one overall graph, using either the par() or layout() function With par() mfrow=c(nrows, ncols) to create a matrix of nrows x ncols plots that are filled in by row. mfcol=c(nrows, ncols) to fill the matrix by columns. # plots filled by row attach(mtcars) opar <- par(no.readonly=TRUE) par(mfrow=c(2,2)) plot(wt,mpg, main="Scatterplot of wt vs. mpg") plot(wt,disp, main="Scatterplot of wt vs disp") hist(wt, main="Histogram of wt") boxplot(wt, main="Boxplot of wt") par(opar) detach(mtcars)
26
opar <- par(no.readonly=TRUE) par(mfrow=c(3,1)) hist(wt) hist(mpg)
#plots filled by row attach(mtcars) opar <- par(no.readonly=TRUE) par(mfrow=c(3,1)) hist(wt) hist(mpg) hist(disp) par(opar) detach(mtcars) #Creating a figure arrangement with fine control opar <- par(no.readonly=TRUE) par(mar = c(4, 4, 2, 2)) par(fig=c(0, 0.8, 0, 0.8)) plot(mtcars$wt, mtcars$mpg, Xlab=”miles Per Gallon”, ylab=“Car Weigth”) par(fig=c(0, 0.8, 0.55, 1), new=TRUE boxplot(mtcars$wt, horizontal=TRUE, axes=FALSE) par(fig=c(0.65, 1, 0, 0.8), new=TRUE) boxplot(mtcars$mpg, axes=FALSE) mtext("Enhanced Scatterplot", side=3, outer=TRUE, line=-3) par(opar) The full graph area as going from (0,0) in the lower-left corner to (1,1) in the upper-right corner fig= parameter is a numerical vector of the form c(x1, x2, y1, y2)
27
Useful functions for working with data objects
length(object) Number of elements/components. dim(object) Dimensions of an object. str(object) Structure of an object. class(object) Class or type of an object. mode(object) How an object is stored. names(object) Names of components in an object. c(object, object,...) Combines objects into a vector. cbind(object, object, ...) Combines objects as columns. rbind(object, object, ...) Combines objects as rows. object Prints the object. head(object) Lists the first part of the object. tail(object) Lists the last part of the object. ls() Lists current objects. rm(object, object, ...) Deletes one or more objects. The statement rm(list = ls()) will remove most objects from the working environment. newobject <- edit(object) Edits object and saves as newobject. fix(object) Edits in place.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.