0%

R-NoteBook

R Lab1 Note

1 Basic Operations

Arithmetic Operations

  1. Define variable

    1
    2
    x <- 10
    y <- 20
  2. Calculation operation

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    x + y
    x * y
    x / y
    x ^ y
    x %% y # the module of x/y
    log(x,y) # The log to the base y of x
    sqrt(x) # the square root of x
    mean(c(x,y)) # the mean between x and y, but remember to use the vector in the mean function!!!
    trunc(x) # discard the decimals of x
    round(x,y) # generally use rounding and keep y decimal places, but will be affected by floating point numbers

    Logical Operations

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
x >= y -3 # arithmetic operations take priority
x <= y
x == y
x != y
w <- TRUE
z <- FALSE
!w # not
w | z # or
w & z # and
isTRUE(operation) # identify if the result is TRUE
identical(x,y) # only when the two objects are the same, it will return TRUE
xor(operation1,operation2) # only when one is TRUE and the other is FALSE, it will return TRUE

# which, any, all, three of them all use logical vector as its argument e.g. vector > 10
which(vector > 10) # It returns the index of all the TRUE elements in the vector.
any(vector > 10) # when one or some elements is TRUE, it will return TRUE
all(vector > 10) # when all the elements are TRUE, it will return TRUE

2 Some additional functions

For example

schoolmath

Here are all its functions

  1. cancel.fraction(numerator, denominator)

    It cancels a fraction to its simplest form

  2. decimal2fraction(decimal, period=0)

    It converts a decimal number into a fraction and the argument period means the repeating ending number if it has.

    E.g. 12.123444444…, so it will be decimal2fraction(12.123,4)

  3. gcd(x, y)

    It gives the greatest common divisor of two numbers, x and y

  4. scm(x, y)

    It gives the smallest common multiple of two numbers, x and y

  5. is. type function

    x can be a number or a vector and it returns TRUE/FALSE

    • is.decimal(x)
    • is.whole(x)
    • is.even(x)
    • Is.odd(x)
    • is.negative(x)
    • is.positive(x)
    • is.real.positive(x) zero is included as a positive number
    • is.prim(x)
  6. prime.factor(x)

    It gives a vector of all the prime-factors (except 1) of a number x

  7. primes(start, end)

    It gives a vector of prime-numbers between start number and end number, if it’s not given start and end, it will give all the prim-numbers

  8. primlist

    It gives a vector of prime-numbers between 1 and 9999999

    e.g.

    1
    primlist[1:10]

    #[1] 1 2 3 5 7 11 13 17 19 23

3 Some Data Structures

There are vectors, sequences, matrices, lists, arrays and data frames.

3-1 Vectors and Sequences

How to define vectors and sequences
  1. Vectors

    Use the c() function, which stands for ‘concatenate’:

    1
    a <- c(1,2,3)

    If you want to create a vector with the same repeated value, use rep(x, times=) or rep(x, each=):

    1
    rep(1,3)

    #[1] 1 1 1

    1
    2
    rep(c(1,2,3),2)
    rep(c(1,2,3),times=2)

    #[1] 1 2 3 1 2 3

    1
    rep(c(1,2,3),each=2)

    #[1] 1 1 2 2 3 3

  2. Sequences

    Use the : operator or the seq(from=, to=, by=) function

    1
    2
    3
    4
    5
    b <- 1:100
    b1 <- seq(1,100)
    b2 <- seq(from=1, to=100)
    b3 <- seq(from=1, to=100, by=1)
    b4 <- seq(to=100, from=1, by=1)

    #all the 5 outputs are same

Some arithmetic and logical operations
  1. Select certain element using index

    1
    a[1]

    #[1] 1

    1
    a[-1]

    #[1] 2 3

    1
    a[c(1,2)]

    #[1] 1 2

    1
    a[1:3]

    #[1] 1 2 3

    For index selection, sequences are the same as vectors.

    NOTICE: In R, the first index is 0 which is totally different from most of other script languages!!!

  2. Logical operations application

    > < == !=

    1
    a>2

    #[1] FALSE FALSE TRUE

    1
    a[a>2]

    #[1] 3

    For logical operations, sequences are the same as vectors.

  3. Arithmetic operations

    In most cases, these operations are performed element by element

    e.g.

    1
    a + 3

    #[1] 4 5 6

    1
    a - -3:-1

    #[1] 4 4 4

    1
    a == 5:7

    #[1] FALSE FALSE FALSE

    1
    sqrt(a)

    #[1] 1.000000 1.414214 1.732051

    1
    mean(a)

    #[1] 2

    1
    sd(a) # standard deviation

    #[1] 1

    1
    median(a) # median

    #[1] 2

    1
    summary(a) # min max mean med quartile infromation

    #Min. 1st Qu. Median Mean 3rd Qu. Max.

    1.0 1.5 2.0 2.0 2.5 3.0

3-2 Matrices

1. How to define matrices
  • Use matrix() function,

matrix(vector, nrow=r, ncol=c, byrow=FALSE, dimnames=list(char_vector_rownames, char_vector_colnames))

The default is the matrix is filled by columns

e.g.

1
2
3
rnames <- c("1st row", "2nd row")
cnames <- c("1st col", "2nd col")
A <- matrix(c(1,2,3,4),nrow=2,byrow=TRUE, dimnames=list(rnames, cnames))
  • Use bind function, rbind() cbind()

    bind by row: rbind(v1, v2, …)

    bind by col: bind(v1, v2, …)

    e.g.

    1
    A <- rbind(c(1,2),c(3,4))

    The two outputs are the same.

2. How to use index

Generally, matrices are 2 dimensions, so [rownumber, colnumber] can locate the certain element in the matrix.

e.g.

1
A[1,2] # the element at the 1st row and 2nd col

If we’d like to get certain whole row or column, we should leave the other dimension blank.

e.g.

1
2
3
4
5
6
A[,2] # 2nd column
A[1,] # 1st row
A[1:2,1] # row 1,2 of column 1
A[c(1,2),c(1,2)] #row 1,2 of column 1,2
A[-1,] # drop 1st row and show other rows
A[A%%2==0] # all the even numbers
3. Arithmetic, logical and statistical operations
  • Mostly, the operations are performed element by element

    + - * / ^

  • arithmetic matrix operation

    e.g. %*% is the arithmetic matrix multiplication

    1
    A %*% B
  • statistical operation

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    sqrt(A)
    mean(A) # mean of all values
    rowMeans(A) # mean by row
    colMeans(A) # mean by column
    sd(A)
    length(A) # the number of all elements
    ncol(A) # the number of columns
    nrow(A) # the number of rows
    rowSums(A) # the sum of each row
    colSums(A) # the sum of each column
  • logical operation

    element by element

    == != > <

3-3 Arrays

1. How to define

Arrays are similar to matrices but can have over 2 dimensions

1
arra <- array(1:8,dim=c(2,2,2), dimnames=list(c('x1','x2'),c('y1','y2'),c('z1','z2'))) # 3D array which is the 2*2*2 structure
2. How to use index
1
2
arra[,,1]
arra[,,'z1']

The two outputs are both the 1st layer of 3rd axis.

3. Operations
1
arra1 <- array(sample.int(100,27), dim=c(3,3,3)) # sample functiongenerates 27 random numbers between 0 and 100

Most of them are the same as matrices, except statistical functions e.g. colMeans(), rowMeans() and rowSums(), colSums()

1
rowMeans(arra1)

The output will be three numbers, of which one is mean of arra1[1/2/3,,]

1
colMeans(arra1)

The output will be a 3*3 matrix, where each element is the mean of arra1[,1/2/3,1/2/3]

4 Simple Plots

How to define

Use plot() function

1
diagram1 <- plot(x, y, col="colorname", type="the type of plot e.g. p/l/c/o/s/h/n", pch="the shape of dot e.g. 0:25", main="title", xlab="X Label", ylab="Y Label", bg="dot color only when pch=21:25", ...)

Finally, there’re still some knowledge I haven’t mentioned, such as data frame and some more complicated applications of all the structures and plot() function. They will be discussed in the following notes later.