\(MOSAIC_{data}\)

Block 0: Introduction to R, Part 3, Solution

Oliver Nakoinz, Lizzie Scholtus, Néhémie Strupler

2022-07-11
License: CC BY-SA 4.0

Exercise

  1. calculate the sum of all numbers from 15 to 75 by
    • using the sum() function and
    • a loop
  2. Write a function, which divides a number a by a number b.
  3. Calculate all length by high values (ore similar numbers) for the fibulae from archdata by
    • using apply(),
    • a simple vector calculation and
    • a loop
  4. write a loop converting the long-form data to wide-form
    • df <- data.frame( key = c(“a”, “a”, “b”, “a”, “b”, “b”, “c”, “c”, “c”), value = c(1, 3, 54, 2, 66, 43, 743, 234, 444) )
    • target: df2 <- data.frame( a = c(), b = c(), c = c() )

Exercise 1: Numbers

Exercise 1: Numbers without loop

sum(15:75)
## [1] 2745

Exercise 1: Numbers with loop

nsum <- 0
numbers <- 15:75
for (i in 1:length(numbers)){
    nsum <- nsum + numbers[i]
}
nsum
## [1] 2745

Exercise 2: Division function

division1 <- function(a, b){
    a / b
}
division1(4,3)
## [1] 1.333333

Exercise 2: Division function

This version is required for apply.

division2 <- function(a){
    a[1] / a[2]
}
a <- c(4,3)
division2(a)
## [1] 1.333333

Exercise 3: Fibulae

The package archdata (Carlson/Roth 2021) provides some archaeological data.

library(archdata)
data("Fibulae")
head(Fibulae)
Grave Mno FL BH BFA FA CD BRA ED FEL C BW BT FEW Coils Length
149 389 93 24 7 10 16 1 13 31 47 3.5 3.5 NA 4 114
190 615 21 7 6 9 6 5 2 11 10 3.5 1.7 NA 12 35
161 125 33 15 2 8 7 3 8 10 20 3.9 3.2 NA 4 60
31 812 23 26 4 7 9 5 12 1 16 6.2 7.7 2.8 4 74
49 798 20 23 2 8 7 1 8 5 16 7.7 5.2 2.6 6 68
6 673 27 15 6 8 7 5 3 11 11 3.7 3.5 1.8 4 55

Exercise 3: Fibulae

fib_df <- data.frame(length = Fibulae$Length, hight = Fibulae$BH)
head(fib_df)
length hight
114 24
35 7
60 15
74 26
68 23
55 15

Exercise 3: apply

apply(fib_df, MARGIN = 1, FUN = division2)
##  [1] 4.750000 5.000000 4.000000 2.846154 2.956522 3.666667 2.812500 2.222222
##  [9] 4.153846 2.294118 3.086957 4.588235 3.133333 3.153846 2.533333 2.750000
## [17] 3.333333 2.769231 2.722222 3.117647 8.533333 3.277778 3.142857 3.133333
## [25] 2.045455 3.533333 2.947368 2.600000 2.153846 6.111111

Exercise 3: vector

fib_df$length / fib_df$hight
##  [1] 4.750000 5.000000 4.000000 2.846154 2.956522 3.666667 2.812500 2.222222
##  [9] 4.153846 2.294118 3.086957 4.588235 3.133333 3.153846 2.533333 2.750000
## [17] 3.333333 2.769231 2.722222 3.117647 8.533333 3.277778 3.142857 3.133333
## [25] 2.045455 3.533333 2.947368 2.600000 2.153846 6.111111

Exercise 3: loop

result <- c()
for (i in 1:nrow(fib_df)){
    result[i] <- fib_df[i,1] / fib_df[i,2]
}
result
##  [1] 4.750000 5.000000 4.000000 2.846154 2.956522 3.666667 2.812500 2.222222
##  [9] 4.153846 2.294118 3.086957 4.588235 3.133333 3.153846 2.533333 2.750000
## [17] 3.333333 2.769231 2.722222 3.117647 8.533333 3.277778 3.142857 3.133333
## [25] 2.045455 3.533333 2.947368 2.600000 2.153846 6.111111

Exercise 4: Long-wide

Assuming that the order of the a-entries, the b-entries and the c-entries is correct and that the different categories belong to three observations.

df <- data.frame(
                key = c("a", "a", "b", "a", "b", "b", "c", "c", "c"),
                value = c(1, 3, 54, 2, 66, 43, 743, 234, 444)
                )
df2 <- data.frame(
                  a = rep(0,3),
                  b = rep(0,3),
                  c = rep(0,3)
                  )

Exercise 4: Long-wide: Loop

ai <- 1
bi <- 1
ci <- 1
for (i in 1:nrow(df)){
    if (df$key[i] == "a") {
        df2$a[ai] <- df$value[i]
        ai <- ai + 1
    }
    if (df$key[i] == "b") {
        df2$b[bi] <- df$value[i]
        bi <- bi + 1
        }
    if (df$key[i] == "c") {
        df2$c[ci] <- df$value[i]
        ci <- ci + 1
        }
}
df2
a b c
1 54 743
3 66 234
2 43 444
Carlson/Roth 2021: D. L. Carlson/G. Roth, Archdata: Example datasets from archaeological research (2021). https://cran.r-project.org/package=archdata.