Printing


In [ ]:
pi

In [ ]:
sqrt(2)

In [ ]:
print(pi)
print(sqrt(2))

In [ ]:
print(matrix(c(1, 2, 3, 4), 2, 2))

In [ ]:
print(list("a", "b", "c"))

cat function

It allows you to cat multiple items. Limitation is it cannot print compound data like list and matrix.


In [ ]:
fib <- c(1, 1, 2, 3, 5)
cat("The fist 5 Fibonaci number is: ", fib, "...\n")

In [ ]:
# cat(list("a", "b", "c"))
# cat(matrix(c(1, 2, 3, 4), 2, 2))

Setting


In [ ]:
x <- 3

In [ ]:
x <- c("fee", "fie", "foo", "fun")
print(x)
  • <<- force the variable to be global

  • Avoid = and -> operator

2.3 Listing


In [ ]:
x <- 10
y <- 50
z <- c("three", "blind", "mice")
f <- function(n, p) sqrt(p/n)
ls()

In [ ]:
ls.str()

In [ ]:
.hidvar <- 10
ls()

In [ ]:
ls(all.names = TRUE)

2.4 Remove


In [ ]:
x <- 2 * pi
x

In [ ]:
rm(x)
ls()

2.5 Creating vector


In [ ]:
c(1, 1, 2, 3, 5, 8, 13, 21)

In [ ]:
v1 <- c(1, 2, 3)
v2 <- c("A", "B", "C")
v3 <- c(v1, v2)

In [ ]:
print(v3)

In [ ]:
print(mode(v1))
print(mode(v2))
print(mode(v3))
  • When combine two vectors, they will be flaten and combined to a single one
  • If they are different types, R will try to convert them to the same type as in the above example.

2.6 Basic stat


In [ ]:
x <- c(0, 1, 1, 2, 3, 5, 8, 13, 21, 34)

In [ ]:
mean(x)

In [ ]:
median(x)

In [ ]:
sd(x)

In [ ]:
var(x)

In [ ]:
y <- log(1+x)

In [ ]:
cor(x, y)

In [ ]:
cov(x, y)

In [ ]:
x <- c(0, 1, 1, 2, 3, NA)
mean(x)
sd(x)

In [ ]:
mean(x, na.rm = TRUE)

In [ ]:
sd(x, na.rm = TRUE)

In [ ]:
str(mtcars)

In [ ]:
dframe <- mtcars[c("mpg", "drat", "wt")]

In [ ]:
str(dframe)
# print(dframe)
colMeans(dframe)

In [ ]:
lapply(dframe, mean)

In [ ]:
lapply(dframe, sd)

In [ ]:
var(dframe)

2.7 creating sequence


In [ ]:
1:5

In [ ]:
seq(from = 1, to = 5, by = 2)

In [ ]:
rep(x = 1, times = 5)

In [ ]:
0:9
10:19
9:0

In [ ]:
seq(from = 0, to = 20, by = 2)

In [ ]:
seq(from = 0, to = 20, by = 5)

In [ ]:
seq(from = 0, to = 20, length.out = 5)

In [ ]:
seq(from = 0, to = 100, length.out = 5)

In [ ]:
seq(from = 0, to = 1.0, length.out = 5)

In [ ]:
rep(pi, times = 5)

2.8 Comparing vectors


In [ ]:
a <- 3
a == pi

In [ ]:
a != pi

In [ ]:
a < pi

In [ ]:
a > pi

In [ ]:
a <= pi

In [ ]:
a >= pi

In [ ]:
v <- c(3, pi, 4)
w <- rep(pi, times = 3)
v < w

In [ ]:
v <= w

In [ ]:
v > w

In [ ]:
any(v == pi)

In [ ]:
all(v > 0)

In [ ]:
all(v == 0)

2.9 Selecting Vector Elements


In [ ]:
years <- c(1960, 1964, 1970, 1994)

In [ ]:
names(years) <- c("Kenndey", "Johnson", "Carter", "Clinton")

In [ ]:
years

In [ ]:
years[c("Kenndey", "Clinton")]

In [ ]:
fib <- c(0, 1, 1, 2, 3, 5, 8, 13, 21)

In [ ]:
fib[1]; fib[2]

In [ ]:
fib[1:3]

In [ ]:
fib[c(1, 2, 4, 8)]; fib[-(1:3)]; fib[-c(1, 2, 4, 8)]

In [ ]:
fib < 10; fib[fib < 10]

In [ ]:
fib %% 2 == 0; fib[fib %% 2 == 0]

In [ ]:
fib > median(fib); fib[fib > median(fib)]

In [ ]:
fib < quantile(fib, 0.26); fib[fib < quantile(fib, 0.26)]

2.10 vector arithmetic


In [ ]:
v <- c(11, 12, 13, 14, 15)
w <- 1:5

In [ ]:
v+w; v - w; v*w; v/w

In [ ]:
w; w+2; w-2; w*2; w/2; w^2; 2^w

In [ ]:
w; w - mean(w); sd(w); (w - mean(w)) / sd(w)

In [ ]: