3 – Lösungen

Statistische Datenanalyse mit R

Autor

Clemens Brunner

Veröffentlicht

16. Oktober 2025

Übung 1

r = 5  # Radius
h = 9  # Höhe
(A = 2 * r * pi * (r + h))  # Oberfläche
[1] 439.823
(V = r**2 * pi * h)  # Volumen
[1] 706.8583

Übung 2

(x = c(4, 18, -7, 16, 4, -44))
[1]   4  18  -7  16   4 -44
(y = x**2)
[1]   16  324   49  256   16 1936
(z = c(x, y))
 [1]    4   18   -7   16    4  -44   16  324   49  256   16 1936
length(z)
[1] 12

Übung 3

x = c(44, 23, -56, 98, 99, 32, 45, 22)
x %% 2 == 0  # logischer Vektor, TRUE bei geraden Elementen
[1]  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE
x[x %% 2 == 0]  # gerade Elemente von x
[1]  44 -56  98  32  22
x %% 2 != 0  # logischer Vektor, TRUE bei ungeraden Elemente
[1] FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE
x[x %% 2 != 0]  # ungerade Elemente von x
[1] 23 99 45

Übung 4

15:40
 [1] 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
seq(75, 61, -3)
[1] 75 72 69 66 63
seq(14, 15, length.out=35)
 [1] 14.00000 14.02941 14.05882 14.08824 14.11765 14.14706 14.17647 14.20588 14.23529 14.26471 14.29412 14.32353
[13] 14.35294 14.38235 14.41176 14.44118 14.47059 14.50000 14.52941 14.55882 14.58824 14.61765 14.64706 14.67647
[25] 14.70588 14.73529 14.76471 14.79412 14.82353 14.85294 14.88235 14.91176 14.94118 14.97059 15.00000

Übung 5

rep(c("Placebo", "Gruppe 1", "Gruppe 2"), each=10)
 [1] "Placebo"  "Placebo"  "Placebo"  "Placebo"  "Placebo"  "Placebo"  "Placebo"  "Placebo"  "Placebo"  "Placebo" 
[11] "Gruppe 1" "Gruppe 1" "Gruppe 1" "Gruppe 1" "Gruppe 1" "Gruppe 1" "Gruppe 1" "Gruppe 1" "Gruppe 1" "Gruppe 1"
[21] "Gruppe 2" "Gruppe 2" "Gruppe 2" "Gruppe 2" "Gruppe 2" "Gruppe 2" "Gruppe 2" "Gruppe 2" "Gruppe 2" "Gruppe 2"

Übung 6

k = seq(0, 20, 2)
k[-c(3, 7)]
[1]  0  2  6  8 10 14 16 18 20
k[1:5]
[1] 0 2 4 6 8
k[c(2, 5, 16)]  # das 16. Element gibt es nicht -> NA
[1]  2  8 NA
k[k > 11]
[1] 12 14 16 18 20

Übung 7

t = c(10, 20, NA, 30, 40)
mean(t)  # Ergebnis ist NA
[1] NA
mean(t, na.rm=TRUE)  # NA-Elemente werden ignoriert
[1] 25

Übung 8

s = c(1, 11.3, 7.8, 3.4, 6)  # Standardabweichungen
s**2  # Varianzen (elementweises Quadrieren)
[1]   1.00 127.69  60.84  11.56  36.00

Übung 9

x = c(2, 0, -5, 0, 1, -1, 0, 3, 0, 0, 7)
x == 0
 [1] FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE  TRUE FALSE
sum(x == 0)
[1] 5
which(x == 0)
[1]  2  4  7  9 10

Übung 10

Innerhalb der eckigen Klammer muss immer exakt ein Vektor sein. Möchte man also drei Positionen indizieren, muss dieser Vektor also mit c() erzeugt werden. Deswegen funktioniert x[2, 4, 6] nicht, weil hier innerhalb der eckigen Klammern drei Vektoren angegeben werden (mit Kommas voneinander getrennt).

Der korrekte Code lautet also:

x = 1:10
x[c(2, 4, 6)]
[1] 2 4 6