Mahbubul Majumder, PhD
Sep 2, 2014
install.packages("package-name")
library("package-name")
sessionInfo()
? plot
How do they work?
x <- "5.42"
is.numeric(x)
[1] FALSE
y <- as.numeric("5.44")
mode(y)
[1] "numeric"
Notice the difference
x <- c(1,2,3)
x
[1] 1 2 3
y <- c(1,2,"3")
y
[1] "1" "2" "3"
mode(y)
[1] "character"
mode()
length(), nrow(), ncol(), dim()
names() rownames() colnames ()
x <- matrix(1:12, ncol=3)
x
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
dim(x)
[1] 4 3
myValue <- mean(1:20)
myValue
[1] 10.5
=
also worksyourValue = mean(1:20)
yourValue
[1] 10.5
c(), seq(), rep(), 1:n, rnorm(), rpois()
x <- seq(1,2.5,by=.5)
x
[1] 1.0 1.5 2.0 2.5
rnorm(mean=2,sd=3, n=3)
[1] 3.766 5.432 1.994
y <- matrix(x,nrow=2)
y
[,1] [,2]
[1,] 1.0 2.0
[2,] 1.5 2.5
z <- list(x, y)
z
[[1]]
[1] 1.0 1.5 2.0 2.5
[[2]]
[,1] [,2]
[1,] 1.0 2.0
[2,] 1.5 2.5
str(z)
List of 2
$ : num [1:4] 1 1.5 2 2.5
$ : num [1:2, 1:2] 1 1.5 2 2.5
c(), rbind(), cbind(), data.frame ()
x <- seq(2, 6, by = 2)
c(x,0,20)
[1] 2 4 6 0 20
rbind(x,y=1:3)
[,1] [,2] [,3]
x 2 4 6
y 1 2 3
zz <- cbind(x,y=1:3)
zz
x y
[1,] 2 1
[2,] 4 2
[3,] 6 3
z <- c("A",1,"B")
d <- data.frame(zz, z)
d
x y z
1 2 1 A
2 4 2 1
3 6 3 B
names(d)
[1] "x" "y" "z"
str(d)
'data.frame': 3 obs. of 3 variables:
$ x: num 2 4 6
$ y: num 1 2 3
$ z: Factor w/ 3 levels "1","A","B": 2 1 3
+ - * /
^ %/% %%
== < > <= >= !=
&& || & | !
9 %/% 4
[1] 2
9 %% 4
[1] 1
9==4
[1] FALSE
! (9==5)
[1] TRUE
(9==5) && (5==5)
[1] FALSE
(9==5) | (5==5)
[1] TRUE
Quiz:
What is the difference between &
and &&
%!%
to obtain geometric mean of two numbers"%!%" <- function(x,y){
res <- x*y
if(res < 0) {
cat("GM is undefined for given values")
}else return(sqrt(res))
}
4%!%9
[1] 6
4%!%(-9)
GM is undefined for given values
x <- rep(1:2,2)
x
[1] 1 2 1 2
x+4
[1] 5 6 5 6
sum(x^2)
[1] 10
y <- matrix(1:9,ncol=3)
y
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
# elementwise operation
y/y
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 1 1 1
[3,] 1 1 1
Quiz:
What would happen for y/c(1,2,3)
mean() median() quantile() sd()
lm() glm() nlme() coef() anova()
dbeta() dbinom() dcauchy() dchisq() dexp() dgamma() dnorm() dpois()
pbeta()
will compute beta probability and rbeta()
will generate random numberx <- rpois(n=5, lambda=2)
x
[1] 0 2 2 2 3
mean(x)
[1] 1.8
ppois(x, lambda=2)
[1] 0.1353 0.6767 0.6767 0.6767 0.8571
x <- matrix(1:12,ncol=3)
x
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
colMeans(x)
[1] 2.5 6.5 10.5
t(x)
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
y <- rep(1,4)
y
[1] 1 1 1 1
y %*% x
[,1] [,2] [,3]
[1,] 10 26 42
diag(x)
[1] 1 6 11
solve() eigen()
also ginv()
which requires MASS packagex <- c(1,4,6,NA)
is.na(x)
[1] FALSE FALSE FALSE TRUE
mean(x)
[1] NA
mean(x, na.rm=T)
[1] 3.667
is.na() na.action() na.omit()
na.rm = T or F
x <- c(3,6,9,NA,3)
x/3
[1] 1 2 3 NA 1
sum(x, na.rm=T)
[1] 21
y <- na.omit(x)
sum(y)
[1] 21
get_square <- function(x){
if(x>100)
return("Big number") else
return(x^2)
}
get_square(5)
[1] 25
get_square(500)
[1] "Big number"
get_square(c(25,200)) # what is wrong here?
[1] 625 40000
foo <- function(x){
print(x)
if(x>1) foo(x-1)
}
moo <- function(x){
if(x>1) moo(x-1)
print(x)
}
for (i in c("A","B", "C")){
if(i > "B") print(i)
}
What is wrong here?
if(4==5) print("A")
else print("B")
if(cond) expr
if(cond) cons.expr else
alt.expr
for(var in seq) expr
while(cond) expr
repeat expr
break
next
read.table() scan() load()
url <- "http://mahbub.stat.iastate.edu/ecg_fractal/ecg_cvp.csv"
dat <- read.table(url, header=T, sep=",")
head(dat)
tm ECG CVP
1 0.000 -0.140 4.781
2 0.003 -0.153 4.781
3 0.006 -0.151 4.831
4 0.008 -0.137 4.831
5 0.011 -0.131 4.880
6 0.014 -0.136 4.930
read.csv() readLines()
? read.table
As soon as you read a data object
head(), tail()
summary(), str()
plot()
head(trees)
Girth Height Volume
1 8.3 70 10.3
2 8.6 65 10.3
3 8.8 63 10.2
4 10.5 72 16.4
5 10.7 81 18.8
6 10.8 83 19.7
plot(trees)
d <- dist(as.matrix(mtcars))
hc <- hclust(d)
groups <- cutree(hc, k = 6)
plot(hc)
rect.hclust(hc, border="blue",k=6)
An Introduction to R
http://cran.r-project.org/doc/manuals/R-intro.pdf
Manual for R Data Import/Export. It describes the import and export facilities available either in R itself or via packages which are available from CRAN http://cran.r-project.org/
Getting help with R, a very helpful documentation provided by RStudio. It also gives the links of nice resources to learn about R. https://support.rstudio.com/hc/en-us/articles/200552336-Getting-Help-with-R
Keep this reference card all the time with you. http://cran.r-project.org/doc/contrib/Short-refcard.pdf