This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.
Creating matrices
# setwd("C:/Users/Manxiaohui/Seurat")
# getwd()
# # y <- matrix(1:20, nrow=5, ncol=4)
# # y
cells <- c(1,26,24,68)
rnames <- c("R1", "R2")
cnames <- c("C1","C2")
mymatrix <- matrix(cells, nrow=2, ncol=2, byrow=TRUE, dimnames=list(rnames,cnames))
mymatrix
## C1 C2
## R1 1 26
## R2 24 68
mymatrix <- matrix(cells, nrow=2, ncol=2, byrow=FALSE,
dimnames=list(rnames, cnames))
mymatrix
## C1 C2
## R1 1 24
## R2 26 68
#Using matrix subscripts
x <- matrix(1:10, nrow=2)
x
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 3 5 7 9
## [2,] 2 4 6 8 10
x[2,]
## [1] 2 4 6 8 10
x[1,4]
## [1] 7
#Listing 2.3 Creating an array
dim1 <- c("A1","A2")
dim2 <- c("B1", "B2","B3")
dim3 <- c("C1","C2","C3","C4")
#creating a dataframe
patientID <- c(1,2,3,4)
age <- c(25,34,28,52)
diabetes <- c("Type1","Type2","Type1","Type2")
status <- c("Poor","Improved", "Excellent","Poor")
patientdata <- data.frame(patientID,age,diabetes,status)
patientdata
## patientID age diabetes status
## 1 1 25 Type1 Poor
## 2 2 34 Type2 Improved
## 3 3 28 Type1 Excellent
## 4 4 52 Type2 Poor
#Specifying elements of a data frame
patientdata[1:2]
## patientID age
## 1 1 25
## 2 2 34
## 3 3 28
## 4 4 52
patientdata[c("diabetes","status")]
## diabetes status
## 1 Type1 Poor
## 2 Type2 Improved
## 3 Type1 Excellent
## 4 Type2 Poor
patientdata$age
## [1] 25 34 28 52
table(patientdata$diabetes,patientdata$status)
##
## Excellent Improved Poor
## Type1 1 0 1
## Type2 0 1 1
#It can get tiresome typing
# at the beginning of every variable name, so
# patientdata$
# shortcuts are available. You can use either the
# and
# or
# attach()
# detach()
# with()
# functions to simplify your code.
# table(patientdata$diabetes,patientdata$status)
# The
# function removes the data frame from the search path. Note that
# detach()
# does nothing to the data frame itself. The statement is optional but is good
# detach()
# programming practice and should be included routinely. (I’ll sometimes ignore this
# sage advice in later chapters in order to keep code fragments simple and short.)
# The limitations with this approach are evident when more than one object can have
#page 28(51 of 474)
attach(mtcars)
summary(mpg)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 10.40 15.43 19.20 20.09 22.80 33.90
# plot(mpg, disp)
plot(mpg, wt)
detach(mtcars)
#
# mpg <- c(25,36,47)
# attach(mtcars)
# plot(mpg,wt)
# mpg
#The limitation of the with()function is that assignments will only exist within the fucntion brackets. Consider the following:
with(mtcars, {
stats <<- summary(mpg)
stats
})
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 10.40 15.43 19.20 20.09 22.80 33.90
stats
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 10.40 15.43 19.20 20.09 22.80 33.90
#if you need to create objects that will exist outside of the which constrcut,use "<<-" instead of <-. it will save the object to the global envrionement outside of the whcih call. see below for the example
with(mtcars, {
nokeepstats <- summary(mpg)
keepstats <<- summary(mpg)
})
# nokeepstats
keepstats
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 10.40 15.43 19.20 20.09 22.80 33.90
#case identifiers
patientdata <- data.frame(patientID,age,diabetes,status,row.names = patientID)
# Factors:
# Categorical(norminal) and ordered categorical (ordinal) variables in R are called factors.
diabetes <- c("Type1", "Type2","Type1","Type1")
#
# #For vectors representing ordinal variables, you add the parameter
# to
# ordered=TRUE
# the
# function . Given the vector
# factor()
status <- c("Poor", "Improved", "Excellent", "Poor")
status <- factor(status, ordered = TRUE)
status <- factor(status, ordered = TRUE, levels = c("Poor","Improved","Excellent"))
status
## [1] Poor Improved Excellent Poor
## Levels: Poor < Improved < Excellent
# Using 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, ordered = TRUE)
patientdata <- data.frame(patientID,age,diabetes, status)
str(patientdata) # why to structure the data
## 'data.frame': 4 obs. of 4 variables:
## $ patientID: num 1 2 3 4
## $ age : num 25 34 28 52
## $ diabetes : Factor w/ 2 levels "Type1","Type2": 1 2 1 1
## $ status : Ord.factor w/ 3 levels "Excellent"<"Improved"<..: 3 2 1 3
summary(patientdata)
## patientID age diabetes status
## Min. :1.00 Min. :25.00 Type1:3 Excellent:1
## 1st Qu.:1.75 1st Qu.:27.25 Type2:1 Improved :1
## Median :2.50 Median :31.00 Poor :2
## Mean :2.50 Mean :34.75
## 3rd Qu.:3.25 3rd Qu.:38.50
## Max. :4.00 Max. :52.00
#2.2.6 list
# Lists are the most complext of the R data types. Basically, a list is an ordered collection of objects (components). A list allows you to gather a variety of (possibly unrelated) objects under one name. For example, a list may contain a combination of vectors, matrices, data frames, and even other lists. You can create a list using the list() funcitons:
# mylist <- list(object1, object2, ...)
# Where the objects are any of the structures seen so far. Optionally, you can name the objects in a list:
# mylist <- list(name1=object1, name2=object2, ...)
### Creating a list
g <- "My First List"
h<- c(25,26,18,39)
j <- matrix(1:10, nrow = 5)
j
## [,1] [,2]
## [1,] 1 6
## [2,] 2 7
## [3,] 3 8
## [4,] 4 9
## [5,] 5 10
k <- c("one", "two","three")
mylist <- list(title=g, ages=h,j,k)
mylist
## $title
## [1] "My First List"
##
## $ages
## [1] 25 26 18 39
##
## [[3]]
## [,1] [,2]
## [1,] 1 6
## [2,] 2 7
## [3,] 3 8
## [4,] 4 9
## [5,] 5 10
##
## [[4]]
## [1] "one" "two" "three"
mylist[[2]]
## [1] 25 26 18 39
mylist[["ages"]]
## [1] 25 26 18 39
length(mylist)
## [1] 4
#Lists are important fucntions in R.
# #Data input
# mydata <- data.frame (age=numeric(0),
# gender=character(0), weight = numeric(0))
# mydata <- edit(mydata)
# Importing data from a delimited text file
getwd()
## [1] "C:/Users/ManXiaohui/Seurat"
# mydataframe <- read.table(SG_lab_confirmation.xls", header = TRUE, sep="\t")
# mydataframe <- read.table()
#
# install.packages("RODBC") # import excel file
# library(RODBC)
# install.packages("XLConnect")
# library(XLConnect)
# channel <- odbcConnectExcel("SG_lab_confirmation.xls")
# mydataframe <- sqlFetch(channel, "mysheet")
# odbcClose(channel)
# channel <- read.table (file="C:/Users/Manxiaohui/Seurat/SG_lab_confirmation",header=TRUE) Readtable does not work,
# attach(mtcars)
# # plot(wt, mpg)
# abline(lm(mpg~wt))
# title("Regression of MPG on Weight")
# detach(mtcars)
# wt
# mpg
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")
opar <- par(no.readonly = TRUE)
par(lty=2,pch=17,family="C")
plot(dose,drugA,type="b")
## Warning in axis(side = side, at = at, labels = labels, ...): font family not
## found in Windows font database
## Warning in axis(side = side, at = at, labels = labels, ...): font family not
## found in Windows font database
## Warning in axis(side = side, at = at, labels = labels, ...): font family not
## found in Windows font database
## Warning in axis(side = side, at = at, labels = labels, ...): font family not
## found in Windows font database
## Warning in axis(side = side, at = at, labels = labels, ...): font family not
## found in Windows font database
## Warning in axis(side = side, at = at, labels = labels, ...): font family not
## found in Windows font database
## Warning in axis(side = side, at = at, labels = labels, ...): font family not
## found in Windows font database
## Warning in axis(side = side, at = at, labels = labels, ...): font family not
## found in Windows font database
## Warning in axis(side = side, at = at, labels = labels, ...): font family not
## found in Windows font database
## Warning in axis(side = side, at = at, labels = labels, ...): font family not
## found in Windows font database
## Warning in title(...): font family not found in Windows font database
## Warning in title(...): font family not found in Windows font database
par(opar)
#Page 51 (74 of 474)
setwd("C:/Users/ManXiaohui/Seurat")
getwd()
## [1] "C:/Users/ManXiaohui/Seurat"
##create plots and saved to PDF
plot(dose, drugA, type="b", lty=2, pch=17)
plot(dose, drugA, type="b", lty=3, lwd=3, pch=15,cex=0.5)
###Try colors
#pdf(file="mydkdkd.pdf", width = 8, height = 8) # This command works (from Google)
n <- 15
mycolors <- rainbow(n)
pie(rep(1,n),labels = mycolors, col=mycolors) # what is rep in R
# names(postscript())
# pdf(file="1.pdf", family = "sans")
# rep(8,4)
# [1] 8 8 8 8
# rep(NA,6)
# [1] NA NA NA NA NA NA
# rep(1:5)
# [1] 1 2 3 4 5
mygrays <- gray(0:n/n)
pie(rep(1,n), labels = mygrays, col=mygrays)
par(font.lab=1, cex.lab=0.5, font.main=4, cex.main=2)
par(pin=c(4,3), mai=c(1,.5, 1, .2))
#dev.off() #create PDF, PDF files can not be replaced, so need to rename when you change parameters to generate NEW PDF file.
# pdf(file="firstPDF.pdf", family = "sans")
# pdf(file="First.pdf", family = ("serif")) does not work, can not open the generated PDF "First.pdf"
names(pdfFonts())
## [1] "serif" "sans" "mono"
## [4] "AvantGarde" "Bookman" "Courier"
## [7] "Helvetica" "Helvetica-Narrow" "NewCenturySchoolbook"
## [10] "Palatino" "Times" "URWGothic"
## [13] "URWBookman" "NimbusMon" "NimbusSan"
## [16] "URWHelvetica" "NimbusSanCond" "CenturySch"
## [19] "URWPalladio" "NimbusRom" "URWTimes"
## [22] "ArialMT" "Japan1" "Japan1HeiMin"
## [25] "Japan1GothicBBB" "Japan1Ryumin" "Korea1"
## [28] "Korea1deb" "CNS1" "GB1"
windowsFonts(
A=windowsFont("Arial Black"),
B=windowsFont("Bookman Old Style"),
C=windowsFont("Comic Sans MS")
)
par(pin=c(1,2))
par(lwd=2,cex=1.5)
par(cex.axis=.75, font.axis=4)
plot(dose, drugA,type="b",pch=19,lty=2,col="red", main="Clinical Trials for Drug A",sub="This is a hypothesis data", xlab = "Dosage", col.main= "red",col.sub="blue", col.lab= "pink",xlim = c(0,60), ylim = c(0,70))
plot(dose, drugA,type="b",pch=19,lty=2,col="red", sub="This is a hypothesis data")
plot(dose, drugB,type="b",pch=23,lty=6,col="blue",bg="green")
par(opar)
# To page 58 (81 of 474)
#Customized plot
x <- c(1:10)
y <- x
z <- 10/x
opar <- par(no.readonly=TRUE)
par(mar=c(5, 4, 4, 8) + 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(side=2, at=x, labels=x, col.axis="red", las=2)
axis(side=4, at=z, labels=round(z, digits=3),
col.axis="blue", las=2, cex.axis=0.7, tck=-.01)
mtext("y=10/x", side=4, line=3, cex.lab=1, las=2, col="blue")
title("An Example of Creative Axes",
xlab="X values",
ylab="Y=X")
#minor.tick(nx=2, ny=3, tick.ratio=0.5) #tick mark
abline(h=c(1,5), lty=2, col="green")
par(opar)
#page 60 (83 of 474)
```