Basic programming with R

Please find an introduction to programming with R here: http://cran.r-project.org/doc/manuals/R-intro.pdf. On https://cran.r-project.org you find links to download R for Windows, MacOS or Linux. One of the most advanced development environments for R is RStudio, see https://www.rstudio.com. Both R and RStudio are free to use and free to download.

In the following, you find a collection of the most useful commands and functions:

Assignments and creating objects

# Numeric: 
index <- c(1, 3); time <- seq(0, 10, length.out=4)
# Named numeric: 
par <- c(K = 0.1, r = 2)
# Character: 
mynames <- c("K", "r")
#Named character: 
mynames <- c(par1 = "K", par2 = "r")
# Numeric matrix:
M <- matrix(1:4, ncol=2, nrow=2)
# Setting row and column names:
colnames(M) <- c("vorne", "hinten")
rownames(M) <- c("ungerade", "gerade")
# Logical:
yes <- TRUE; no <- FALSE
# Lists:
mylist <- list(A = matrix(1:4, 2, 2), v = c(3, 4))

Combining objects

# Concatenate two vectors
c(index, index)
# Binding vectors together by columns
cbind(index, index)
# Binding vectors together by rows
rbind(index, index)
# Do the same for matrices
cbind(M, M)
rbind(M, M)

Accessing objects

# By index
time[c(1,3)]; time[index]; time[1:3]
# In matrices
M[1,1]; M[,1]; M[1,]
# In lists
mylist[[1]]; mylist[[2]]
# By names
par["K"]; mynames["par1"]; M[,"vorne"]; M["ungerade",]
# In lists
mylist$A; mylist[["A"]]; mylist$v; mylist[["v"]]
# Attention: for lists single brackets return a list of the length of the index vector
mylist["A"]; mylist[1]

Request attributes of an object and print information about the object

# List all attributes
attributes(M)
# Access specific attribute
attr(M, "dimnames")
# Specialized commands to obtain attributes
length(time)
dim(M)
names(par)
colnames(M)
rownames(M)
# Print information about the structure of the object
str(M)

Operations

# Operations element by element
M+M; M*M; 1/M; M*par
# Matrix operations
M+M; M%*%M; solve(M); M%*%solve(M); M%*%par; t(M)
# Logical operations
no <- !yes; alwaysTrue <- (yes | no); alwaysFalse <- (yes & no)

Loops and if statements

# Basic "for" loop
v <- numeric(5)
for (i in 1:5) {
  if (i != 3) {
    v[i] <- i^2  
  } else {
    v[i] <- NA
  }
}

Testing objects

# find missing values
is.na(v)
# return position within a vector
which(is.na(v))
which(v == 4)
# check object type
is.numeric(v)
is.character(v)

Functions

myfunc <- function(x, a) {
  y1 <- a*x^2
  y2 <- sum(x)
  y3 <- y1+y2
  return(y3)
}

myx <- seq(0,1,by=0.1)
mya <- 3
values <- myfunc(myx, mya)
print(values)

How to avoid loops

## Make use of vectorized functions
v <- 1:5; w <- 6:10
# Very bad (slow!):
result <- NULL
for (i in 1:5) {
  result <- c(result, v[i]*w[i])
}
# better:
result <- numeric(5)
for (i in 1:5) {
  result[i] <- v[i]*w[i]
}
# correct:
result <- v*w

## Make use of lapply()
v <- lapply(1:5, function(i) i^2)
## What to do with a list?
# Convert to a vector
unlist(v)
# Call other functions
do.call(rbind, v)
# Reduce by binary operators or functions
Reduce("+", v)
Reduce(function(x, y) x+y, v)