Aufgabe 2

aspettl — May 10, 2013, 2:00 PM

load("tatort2013.rda")

# Variante 1 mit Schleifen und is.na:

# a)
plot(tatort2013$Schaetzung$personC)
for (i in seq(along=tatort2013$Schaetzung$Geschlecht)) {
    if (is.na(tatort2013$Schaetzung$Geschlecht[i])) {
        # nichts zu tun
    } else {
        if (tatort2013$Schaetzung$Geschlecht[i] == 'M') {
            points(i, tatort2013$Schaetzung$personC[i], col="blue")
        } else if (tatort2013$Schaetzung$Geschlecht[i] == 'W') {
            points(i, tatort2013$Schaetzung$personC[i], col="red")
        } else {
            points(i, tatort2013$Schaetzung$personC[i], col="green")
        }
    }
}
# b)
abline(tatort2013$Alter[3], 0, lwd=2)
abline(mean(tatort2013$Schaetzung$personC), 0, lty=2, lwd=2)

plot of chunk unnamed-chunk-1

# c)
plot(tatort2013$Schaetzung$personC) 
for (i in seq(along=tatort2013$Schaetzung$Geschlecht)) {
    if (is.na(tatort2013$Schaetzung$Geschlecht[i])) {
        # nichts zu tun
    } else {
        if (!is.na(tatort2013$Schaetzung$Tatort[i]) && tatort2013$Schaetzung$Tatort[i] == 'J') {
            if (tatort2013$Schaetzung$Geschlecht[i] == 'M') {
                points(i, tatort2013$Schaetzung$personC[i], col="blue", pch=19)
            } else if (tatort2013$Schaetzung$Geschlecht[i] == 'W') {
                points(i, tatort2013$Schaetzung$personC[i], col="red", pch=19)
            } else {
                points(i, tatort2013$Schaetzung$personC[i], col="green", pch=19)
            }
        } else {
            if (tatort2013$Schaetzung$Geschlecht[i] == 'M') {
                points(i, tatort2013$Schaetzung$personC[i], col="blue")
            } else if (tatort2013$Schaetzung$Geschlecht[i] == 'W') {
                points(i, tatort2013$Schaetzung$personC[i], col="red")
            } else {
                points(i, tatort2013$Schaetzung$personC[i], col="green")
            }
        }
    }
}

plot of chunk unnamed-chunk-1



# Variante 2 ohne Schleifen, aber Ausschnitten des Data-Frame-Objekts:

# a)
schaetzungOhneNA <- subset(tatort2013$Schaetzung, !is.na(Geschlecht))
# alternativ: schaetzungOhneNA <- tatort2013$Schaetzung[!is.na(tatort2013$Schaetzung$Geschlecht),]
schaetzungM <- subset(schaetzungOhneNA, Geschlecht=='M')
# alternativ: schaetzungM <- schaetzungOhneNA[schaetzungOhneNA$Geschlecht=='M',]
schaetzungW <- subset(schaetzungOhneNA, Geschlecht=='W')
schaetzungMW <- subset(schaetzungOhneNA, Geschlecht=='MW')
plot(tatort2013$Schaetzung$personC)
points(rownames(schaetzungM), schaetzungM$personC, col="blue")
points(rownames(schaetzungW), schaetzungW$personC, col="red")
points(rownames(schaetzungMW), schaetzungMW$personC, col="green")

plot of chunk unnamed-chunk-1


# Variante 3 mit Levels, aus denen die Farben abgeleitet werden:

# a)
str(tatort2013$Schaetzung$Geschlecht)  # Darstellung als Levels, Level 1 = M etc.
 Factor w/ 3 levels "M","MW","W": 1 1 1 1 1 1 1 1 1 1 ...
colors <- as.numeric(tatort2013$Schaetzung$Geschlecht)+1 # konvertiere Level-Zahlen in Farben
plot(tatort2013$Schaetzung$personC)
points(tatort2013$Schaetzung$personC, col=colors) # Farbe NA wird automatisch nicht gezeichnet, diese Punkte sind aber vom plot-Befehl noch da

plot of chunk unnamed-chunk-1

# Alternative:
# Ein einziger Plot-Befehl reicht, wenn man die NA-Einträge auch in Levels umwandelt:
colors <- as.numeric(addNA(tatort2013$Schaetzung$Geschlecht))+1
plot(tatort2013$Schaetzung$personC, col=colors)

plot of chunk unnamed-chunk-1

# c)
colors <- as.numeric(addNA(tatort2013$Schaetzung$Geschlecht))+1
chars <- as.numeric(addNA(tatort2013$Schaetzung$Tatort))+14
plot(tatort2013$Schaetzung$personC, col=colors, pch=chars)

plot of chunk unnamed-chunk-1