Index | About | Me | Jump to Menu Section

R lang

Rmd

Resources to learn

Basic commands

Atomic Objects

Vectors

> x <- c(1,2)
> names(x) <- c("aa", "bb")
> x
aa bb
1 2
> x <- c(1,2,3,4) # concat vectors

Factors

> factor(c("A", "B", "C"), labels = c("A", "B", "C"), ordered = T)
[1] A B C
Levels: A < B < C
factor(x = character(), levels, labels = levels,
exclude = NA, ordered = is.ordered(x), nmax = NA)

> factor(c("A", "B", "C"))
[1] A B C
Levels: A B C

Matrixes

> matrix(data = 1:6, nrow = 2, ncol = 3)
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6

> attributes(matrix( data = 1:6, nrow = 2, ncol = 3))
$dim
[1] 2 3

> x <- c(1,2,3,4)
> dim(x) <- c(2,2)
> x
[,1] [,2]
[1,] 1 3
[2,] 2 4

List

Data frames

Subcollections

> x <- 1:4; x[-2]
[1] 1 3 4
 > x <- 1:4; x[x > 2]
[1] 3 4
> x <- c(1,2); names(x) <- c("aa", "bb"); x["aa"]
aa
1
> m[2, ] # second line
> m[, 3] # third column
> m[, 3, drop=FALSE] # third column and returns it as a column
> x <- list(foo = 1:4, bar = 1); x$f
[1] 1 2 3 4

Dates

> unclass(as.Date('1976/01/01'))
[1] 2191

Timestamps

Curious stuff

> 1/0
[1] Inf
> 0/0
[1] NaN

Random

> 10:20
[1] 10 11 12 13 14 15 16 17 18 19 20
> seq(from=7, by=4, to=20)
[1] 7 11 15 19
> x <- runif(5);
> x
[1] 0.03387367 0.53094936 0.26855677 0.96293228 0.01368555
> order(x)
[1] 5 1 3 2 4

Functions

Good to know

Reading data

ggplot2