## ----style, echo = FALSE, results = 'asis'--------------------------------------------------------
BiocStyle::markdown()
options(width=100, max.print=1000)
options(useFancyQuotes=FALSE)
knitr::opts_chunk$set(
    eval=as.logical(Sys.getenv("KNITR_EVAL", "TRUE")),
    cache=as.logical(Sys.getenv("KNITR_CACHE", "TRUE")), 
    error=FALSE)

## -------------------------------------------------------------------------------------------------
mean(1:10)
rnorm(1:10)
summary(rnorm(1:10))

## -------------------------------------------------------------------------------------------------
data(iris) 

# find those rows where petal.width is exactly 0.2
iris[iris$Petal.Width==0.2,]

# find those rows where sepal.length is less than 4.5
iris[iris$Sepal.Length < 4.5,]

# find all rows belonging to setosa
setosa_iris = iris[iris$Species=="setosa",]
dim(setosa_iris)
head(setosa_iris)

## -------------------------------------------------------------------------------------------------
# drop the column containing characters i.e., Species 
iris <- iris[,!( names(iris) %in% "Species")]
dim(iris)

# find the mean of the first 4 numerical columns 
lapply(iris, mean) # simpler: colMeans(iris)

# simplify the result 
sapply(iris, mean)

# find the mean for each row. 
apply(iris, 1 , mean) #simpler : rowMeans(iris)

## -------------------------------------------------------------------------------------------------
# define a vector
x <- rnorm(1000) 

# vectorized calculation
y <- x + rnorm(1000, sd=.8) 

# object construction
df <- data.frame(x=x, y=y)
 
# linear model
fit <- lm(y ~ x, df)

## -------------------------------------------------------------------------------------------------
par(mfrow=c(1,2))
plot(y ~ x, df, cex.lab=2)
abline(fit, col="red", lwd=2)

library(ggplot2)
ggplot(df, aes(x, y)) + 
   geom_point() +
   stat_smooth(method="lm")

## ----sessionInfo----------------------------------------------------------------------------------
sessionInfo()