#### Interaction with R # Use for mathematical operations 8 + 8 (8 + 5)/14 2^(83/14) # Assign an object x <- 15 y <- 20 x + y u = 20 u + 15 15 == 20 z <- "Hello world!" zz <- 'Hello world!' # R IS CASE-SENSITIVE! NA JA Z <- 5 z Z # Overwriting x x <- 5 x # Workspace ls() rm("zz") ls() rm(x) ls() rm(list = ls()) ls() gc() # Use after remove if you have a big data file. It collects memories from different parts into the same place. # Vector x <- c(1, 3, 8, 9) #Concatenate x/2 y <- c(2, 8, 9, 1) x + y z <- c(1, 9, 5, 6, 7) x + z w <- c(x, y) w[4] w[3] w[8] w[c(2, 4)] x[c(1, 3)] x[c(1, 5)] u <- c(1, NA, 2, 3) u[c(1, 4, 2, 5)] sin(3.142857) sqrt(x) mean(x) sd(x) min(x) max(x) range(x) ww <- range(x) ww[2] - ww[1] var(x) length(x) log(x) sum(x) aa <- 8.53467 round(aa, 2) ceiling(aa) floor(aa) newx <- c(x, y) sort(newx) sort(newx, decreasing = TRUE) order(newx) # Exercise 1.1 a <- 1:15 mean(a) sd(a) a - mean(a) (a - mean(a)) / sd(a) # Other types of R objects # Character z <- "Hello World!" nchar(z) m <- "c:\windows" n <- "c:/windows" mm <- "c:\\windows" txt <- "My money is gone.\n \tI do not know how to live bla bla blah." mm <- c("abc", "efg", "ijklmn") # Logical x <- TRUE y <- FALSE xx <- T yy <- F 3 > 2 4 > 1 nchar(z) < 10 length(mm) == 5 8 != 5 # Exercise 1.2 ex12 <- seq(4, 100, 7) ex12 ex12%%5 == 0