DongwooJeong / r-study

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

R study

This repository is for studying R programming, based on the youtube channel Kee-Young Kwahk.


Vector (Lecture 7)

Youtube Link

  • how to create a vector: in a form of c()
c(1,2,3)
c('a','b','c')
c(TRUE,FALSE)
  • assign a value(vector) into a variable: use '=' or '<-'
vec1 = c(1,2,3)
vec2 <- c(TRUE,FALSE)
  • Using seq function to create a sequence
    • seq(from=x, to=y): create a sequence starting from x to y by 1
      • by=z: increment of z
      • length.out: desired length of the sequence
seq(from=0, to=6, by=2)
seq(from=1, to 2, length.out)
  • replicate value: rep()
    • rep(x, times=3): repeat x 3 times
      • each=y: repeat each element in the vector
      • times=c(a,b,c): each element of x is repeated each value of the second vector in order separately
      • length.out: desired length of the output vector
rep(c(1,2,3), times=3)
rep(c(1,2,3), each=3)
rep(c(1,2,3), times=c(1,2,3))
rep(c(1,2,3), length.out=8)
  • when combining numeric vector and character vector, the elements in the numeric vector is converted into a character vector

  • str(): see the structure of the vector

  • comma is used to represent the dimension of the vector

  • length(): print the magnitude of the vector

  • indexing: specifies element in the vector using brackets

vec2 = c(1,2,3)
vec2[1]
  • built-in vectors: LETTERS, letters, month.name, month.abb, pi

Vector Calculation (Lecture 8)

Youtube Link

  • Unlike other programming languages, R uses a vector calculation to calculate.
  • in 1+2, 1 and 2 is a vector and + is a function
  • vector calculation is performed in an element unit.
c(1, 2, 3) + c(4, 5, 6)
  • If two vectors are not in the same length, recycling rule applies.
  • recycling rule: In a vector calculation of different length of vectors, R reuse the element of the shorter vector
c(1, 2, 3) + c(4, 5, 6, 7, 8, 9)
  • Warning message when the length of the longer vector is not a multiple of a shorter vector
c(1, 2, 3) + c(4, 5, 6, 7, 8)
  • logical operation
    • ==, !=, <, >, <=, >=, !x, x|y, x&y, isTRUE()
  • logical operation using vector calculation
x <- c(1,2,3,4,5)
y <- c(3,3,3,3,3)
x > y
  • same as comparing with scalar
x <- c(1,2,3,4,5)
x > 3
  • logical operator as a number
    • TRUE: 1
    • FALSE: 0
    as.numeric(TRUE)
    TRUE*TRUE
  • functions using logical vectors
    • sum(): number of the elements that satisfy the condition
    • any(): print TRUE if any element satisfies the condition
    • all(): print TRUE if all elements satisfy the condition
    y = c(0, 25, 50, 75, 100)
    sum(y>50)
    any(-3:3 > 0)
    all(-3:3 > 0)
  • unique characteristic of ==
    • floating point number: In some cases, the approximation of the real number is expressed using rounding, so even two equations representing the same number are expressed as not the same
    • use all.equal() to ignore a slight difference
    sqrt(2)^2 == 2
    identical(sqrt(2)^2,2)
    all.equal(sqrt(2)^2,2)
  • vector calculation with character vectors
    • paste(): combine two character vectors like numeric vectors
      • recycling rule also applies
    fruit = c("apple", "banana", "strawberry")
    food = c("pie", "juice", "cake")
    paste(fruit, food)
    paste(fruit, "juice")

Vector function (Lecture 9)

Youtube Link

  • R functions also use vectorized way to perform calculation
  • absolute value
abs(-3:3)
  • logarithm
log(1:5)
log(1:5, base=exp(1))

log2(1:5)
log10(1:5)
  • factorial and combination
factorial(1:5)
choose(5, 2)
  • square root
sqrt(1:5)
  • significant value, default number is 7
options("digits")
signif(456.789, digits=2)
signif(456.789, digits=3)
signif(456.789, digits=4)
  • rounding
round(456.789, digits=2)

round(456.789)
round(sqrt(1:5),digits=2)
round(456.789, digits=-2)

round(11.5)
round(-5.5)

floor(456.78)
floor(-456.78)

ceiling(456.78) 
ceiling(-456.78)

trunc(456.78)
trunc(-456.78)

~ 14:50

About


Languages

Language:R 100.0%