4 min read

R Basic Code

Here are some useful r codes to implement:

Mathematical functions

log(3)
## [1] 1.098612
exp(2)
## [1] 7.389056
sqrt(4)
## [1] 2
#Rounding
round(3.78,1)
## [1] 3.8
#Absolute Value
abs(-3)
## [1] 3

Special Values

#NA multiplication
5*NA
## [1] NA
#Nan : Not a Number
0/0
## [1] NaN
x <- NaN
#Check whether x is na
is.na(x)
## [1] TRUE
x == NA
## [1] NA

Data Structure

Vectors

#vector expression
c(2,4,5,6)
## [1] 2 4 5 6
#sequence
seq(1,10,by=3) 
## [1]  1  4  7 10
#repetition
rep(1:4,each=2,times=3)
##  [1] 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4
x <- c(1,2,3,4,5)
#indexing
x[2:3]
## [1] 2 3
x[-(3:4)]
## [1] 1 2 5
v <- c(2,3,4,5,6)
#put a new number
v[v>4] <- 0
v
## [1] 2 3 4 0 0

Vector Calculation

#Add
c(2,3,4) + c(5,6,7)
## [1]  7  9 11
x <- c(2,3,4)
#expressions
max(x)
## [1] 4
quantile(x)
##   0%  25%  50%  75% 100% 
##  2.0  2.5  3.0  3.5  4.0
tmp <- 0:10
sum(tmp)
## [1] 55
#partial sum
sum(tmp[tmp<5])
## [1] 10
v <- c(3,1,2,17,9,19)
#sorting
sort(v)
## [1]  1  2  3  9 17 19

Matrix

# byrow=True : row direction, names set the name for row and col
x <- matrix(1:8,nrow=4,ncol=2,byrow=TRUE)
rownames(x) <-c("r1","r2","r3","r4")
colnames(x) <-c("c1","c2")
x
##    c1 c2
## r1  1  2
## r2  3  4
## r3  5  6
## r4  7  8
# cbind: binding through column
cbind(1:3,5:7)
##      [,1] [,2]
## [1,]    1    5
## [2,]    2    6
## [3,]    3    7
# rbind: binding through row
rbind(1:2,3:4)
##      [,1] [,2]
## [1,]    1    2
## [2,]    3    4
# matrix indexing
x[3,2]
## [1] 6
x[,2]
## r1 r2 r3 r4 
##  2  4  6  8
x[-c(1,3),]
##    c1 c2
## r2  3  4
## r4  7  8

Matrix Calculation

m1 <- matrix(1:4,nrow=2)
m2 <- matrix(5:8,nrow=2)
#Add
m1+m2
##      [,1] [,2]
## [1,]    6   10
## [2,]    8   12
#Multiply element-wise
m1*m2
##      [,1] [,2]
## [1,]    5   21
## [2,]   12   32
#inner product
m1%*%m2
##      [,1] [,2]
## [1,]   23   31
## [2,]   34   46
#diagonal matrix
diag(3)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1
diag(1:4)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    2    0    0
## [3,]    0    0    3    0
## [4,]    0    0    0    4
#apply the equation, 1:row, 2:column
apply(m1,1,sum)
## [1] 4 6
apply(m2,2,prod)
## [1] 30 56
#inverse
solve(m1)
##      [,1] [,2]
## [1,]   -2  1.5
## [2,]    1 -0.5
#determinant
det(m1)
## [1] -2

Factors

# factor makes vector as categorical data
x <- c(3,2,1,3,1,2,3,3,1,2,3,3,1)
factor(x)
##  [1] 3 2 1 3 1 2 3 3 1 2 3 3 1
## Levels: 1 2 3
factorized <- factor(x,levels=c(0,1,2,3),labels=c("a","b","c","d"))
factorized
##  [1] d c b d b c d d b c d d b
## Levels: a b c d

Lists

#List can have different types of data in one
L <- list (name=c('A','B'),age=c(25,24),color=c('red','blue'))
L
## $name
## [1] "A" "B"
## 
## $age
## [1] 25 24
## 
## $color
## [1] "red"  "blue"
L[[2]][1]
## [1] 25
L2 <- list(v=c(1,2,3),m=matrix(1:6,nrow=3),text=c("Hello","world"))
L2
## $v
## [1] 1 2 3
## 
## $m
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6
## 
## $text
## [1] "Hello" "world"

Dataframe

group <- data.frame(name=c("A","B","C","D","E"),
                    gender=c("M","F","F","M","F"),
                    color=c("red","blue","green","yellow","pink"),
                    income=c("100","200","300","400","500"))
group
##   name gender  color income
## 1    A      M    red    100
## 2    B      F   blue    200
## 3    C      F  green    300
## 4    D      M yellow    400
## 5    E      F   pink    500
#summary
summary(group)
##  name  gender    color   income 
##  A:1   F:3    blue  :1   100:1  
##  B:1   M:2    green :1   200:1  
##  C:1          pink  :1   300:1  
##  D:1          red   :1   400:1  
##  E:1          yellow:1   500:1
#indexing [row,column], - means remove
group$income
## [1] 100 200 300 400 500
## Levels: 100 200 300 400 500
group$gender[2]
## [1] F
## Levels: F M
group[,"income"]
## [1] 100 200 300 400 500
## Levels: 100 200 300 400 500