> x = 10.5 # assign a decimal value
> x # print the value of x
[1] 10.5
> class(x) # print the class name of x
[1] "numeric"
> y = as.integer(3)
> y # print the value of y
[1] 3
> class(y) # print the class name of y
[1] "integer"
> z = 1 + 2i # create a complex number
> z # print the value of z
[1] 1+2i
> class(z) # print the class name of z
[1] "complex"
> x = 1; y = 2 # sample values
> z = x > y # is x larger than y?
> z # print the logical value
[1] FALSE
> class(z) # print the class name of z
[1] "logical"
> x = as.character(10.5)
> x # print the character string
[1] "10.5"
> class(x) # print the class name of x
[1] "character"
> B = matrix(
+ c(2, 4, 3, 1, 5, 7),
+ nrow=3,
+ ncol=2)
> B # B has 3 rows and 2 columns
[,1] [,2]
[1,] 2 1
[2,] 4 5
[3,] 3 7
list
> n = c(2, 3, 5)
> s = c("aa", "bb", "cc", "dd", "ee")
> b = c(TRUE, FALSE, TRUE, FALSE, FALSE)
> x = list(n, s, b, 3) # x contains copies of n, s, b
data frame
> n = c(2, 3, 5)
> s = c("aa", "bb", "cc")
> b = c(TRUE, FALSE, TRUE)
> df = data.frame(n, s, b) # df is a data frame
> df
n s b
1 2 aa TRUE
2 3 bb FALSE
3 5 cc TRUE
3. install.packages
> install.packages("PACKAGE")
# using bioconductor.org
> source("https://bioconductor.org/biocLite.R")
> biocLite("PACKAGE")
Common used packages
To load data:
RMySQL: read in data from a database or Excel.
xlsx: read in data from a Excel.
To manipulate data:
stringr: easy to learn tools for regular expressions and character strings.
reshape2: transform data between wide and long formats.
To visualize data:
gplots: heatmap.2
ggplot2: plotting system for R, based on the grammar of graphics
plotly: R package for creating interactive web-based graphs
To model data:
lme4, nlme: Linear and Non-linear mixed effects models
randomForest: Random forest methods from machine learning
glmnet: Lasso and elastic-net regression methods with cross validation