---
title: "Solutions to exercise sheet 3 - Data types and structures"
output: pdf_document
pointsize: 10
---

## Exercise 1 - Defining vectors

```{r}
v1 <- c(3, 7, -4, 0)
v2 <- (1:50)
# Do not forget the c() function!
v3 <- c(3, 7, -4, 0:50)
v3 <- c(v1, v2)
v4 <- seq(from=0.0, to=2.0, by=0.1)
sum(v1)
sum(v2)
prod(10:40)
```

## Exercise 2 - Looking at vector elements

```{r}
# Define the vector data 
data <- 90*1:100 - (1:100)^2 + 1000
# 1st entry
data[1]
# 17th entry
data[17]
# Last entry
data[length(data)]
# Length of the vector
length(data)
# What is the maximum of the vector data? At which index is the maximum attained?
max(data)
which.max(data)
# Plot the vector data and visually confirm your last result
plot(data)
# At which indices are the entries of data between 2000 and 2500?
some_values <- (data >= 2000 & data <= 2500)
which(some_values)
# When you are interested in getting the values, you can now access these indices:
indices <- which(some_values)
data[indices]
# Or:
data[which(some_values)]
# Define the vector half that contains only the last half of the elements of data
half <- data[-1:-50]
```

## Exercise 3 - Lists 

```{r}
myList <- list(1:6, c("a", "b"), c(FALSE, TRUE, TRUE))
# Element with index 2
myList[2]
# Type of element with index 3
class(myList[3])
# Compare with
class(myList[[3]])
``` 

## Exercise 4 - Matrices

```{r}
m <- matrix( 11:35, nrow=5, byrow=TRUE )
# Entry in the third row and forth column
m[3,4]
# Submatrix - row 2-4, column 3-5
sub <- m[2:4,3:5]
sub
# Assign column names
colnames(sub) <- c("Variable1", "Variable2", "Variable3")
sub
``` 

\pagebreak

## Exercise 5 - Matrices

```{r}
# Note that I assigned variable names (m1 - m7) to the matrices
m1 <- matrix(data=1:6, nrow=2, ncol=3)
m1
m2 <- matrix(data=rep(0,6), nrow=2, ncol=3)
m2 <- matrix(0,nrow=2, ncol=3)
# If you know the order of the parameters (data,nrow, ncol), even this works
# but caution: the order is important!
m2 <- matrix(0,2,3)
m2
m3 <- matrix(data=rep(1,6), nrow=2, ncol=3)
m3 <- matrix(1,nrow=2, ncol=3)
# If you know the order of the parameters (data,nrow, ncol), even this works
# but caution: the order is important!
m3 <- matrix(1,2,3)
m3
m4 <- matrix(data=c(2:4,7:9), nrow=2, ncol=3, byrow = TRUE)
m4 <- rbind(c(2:4),c(7:9))
m4
m5 <- diag(1,4,4)
m5 <- diag(1,4)
# As we have the identity (or unit matrix) here, i.e. only 1 in the diagonal 
# and 0 everywhere else, # this also works:
m5 <- diag(4)
m5
# Matrix m6 can be created using a small trick.
# Look at these 2 matrices, the 1st matrix only contains 1 at each position, 
# the 2nd matrix is the square identity matrix of size 4 (see m5):
matrix(1,4,4)
diag(4)
# Now, we can define m6 using these 2 matrices
m6 <- matrix(1,4,4) - diag(4)
m6
m7 <- matrix(rep(c(1:4)^2,4), nrow=4, byrow=TRUE)
# Or
m7 <- matrix(rep(c(1,4,9,16),4), nrow=4, byrow=TRUE)
m7
``` 

## Exercise 6 - Data frames

```{r}
# Define matrix column by column
name <- c("Leonie", "Luca", "Leon", "Lea", "Luis", "Laura")
degree <- c("Bachelor", "Master", "Bachelor", "Bachelor", "Master", "Master")
grade <- c(2.3, 3.0, 2.0, 1.3, 2.7, 1.0)
results <- data.frame(name, degree, grade, stringsAsFactors = FALSE)
```

```{r}
# Or everything in one go
results <- data.frame(name = c("Leonie", "Luca", "Leon", "Lea", "Luis", "Laura"), 
                     degree = c("Bachelor", "Master", "Bachelor", "Bachelor", "Master", "Master"), 
                     grade = c(28, 14, 19, 27, 13, 10), 
                     stringsAsFactors = FALSE)
names(results)
summary(results)
str(results)
# Extract column 'grade'
results$grade
# 5th element of vector 'grade'
results[5, 3]
# Define data frame students
students <- results[c("name", "degree")]
# Change column name
names(results)[2] <- "deg"
names(results)
``` 

Last update: `r as.Date(Sys.time())`
