## R-Einfuehrung NDK ## -------------------------------------------------------------- ## Daten einlesen d.sport <- read.table("sport.dat",header=TRUE) d.sport d.sport[,"kugel"] d.sport[5,] ## Grafik -- Funktionsaufruf hist(d.sport[,"kugel"]) hist(d.sport[,"kugel"], nclass=10) hist(d.sport[,"kugel"],,,,,,,,,,,,,,,,10) objects() ls() # q() ## Grafik plot(d.sport[,"kugel"], d.sport[,"speer"]) pairs(d.sport) # dev.print() ## Hilfe help(hist) ?hist example(hist) ## Fehlermeldungen ## -------------------------------------------------------------- ## Vektoren t.v <- d.sport[,"kugel"] t.a <- c(3.1, 5, -0.7, 0.9, 1.7) seq(0,3,by=0.5) 1:9 rep(0.7,5) rep(c(1,3,5),length=8) ## Alphanumerische Vektoren t.b <- c("Andi" "Bettina", "Christian") paste("ABC","XYZ",17) paste("ABC","IJK","XYZ",sep=":") paste(c("a","b","c"),1:3) paste(letters, collapse="; ") # paste hat sie beliebig viele Argumente! ## Logische Vektoren (1:5)>=3 t.i <- (t.v>2)&(t.v<5) ## -------------------------------------------------------------- ## Arithmetik 2+5 (2:5)^c(2,3,1,0) (2:5)^2 (1:5)-(0:1) ## -------------------------------------------------------------- ## Elemente auswählen t.v[c(1,3,5)] d.sport[c(1,3,5),1:3] t.a[c(TRUE,FALSE,TRUE,TRUE,FALSE,FALSE)] d.sport[c("OBRIEN","DVORAK"),c("kugel","speer","punkte")] d.sport$kugel ## -------------------------------------------------------------- ## Verteilungen und Zufallszahlen dbinom(0:5, size=5, prob=0.7) pbinom(0:5, size=5, prob=0.7) qbinom(seq(0.1,0.9,0.1),size=5,prob=0.7) rbinom(20, size=5, prob=0.7) rpois(20, lambda=3.5) runif(4) runif(2) set.seed(27) runif(4) set.seed(27) runif(2) plot(0:15,dpois(0:15,lambda=3.5), type="h", lwd=3) t.x <- seq(0,10,length=100)[-1] plot(t.x,dnorm(t.x, 5, 2), type="l", xlab="x", ylab="Dichte", main="Normalverteilung") plot(t.x,dlnorm(t.x, log(3), log(1.7)), type="l", xlab="x", ylab="Dichte", main="Log-Normalvert.") ## -------------------------------------------------------------- ## Einfache Statistik-Funktionen binom.test(3,20, p=0.4) t.x <- 5 1-ppois(t.x-1,lambda=2.7) t.hh <- d.sport[,"hoch"]>200 t.kugel <- d.sport[,"kugel"] wilcox.test(t.kugel[t.hh],t.kugel[!t.hh]) ## Grafische Darstellungen für zwei Stichproben. boxplot(t.kugel[t.hh],t.kugel[!t.hh],notch=TRUE) t.br <- 13:17 t.h1 <- hist(t.kugel[t.hh],breaks=t.br, plot=FALSE) t.h2 <- hist(t.kugel[!t.hh],breaks=t.br, plot=FALSE) barplot(rbind(t.h1$density,t.h2$density), beside=TRUE, names.arg=t.h1$mid) ## -------------------------------------------------------------- ## Funktionen f.maxi <- function(data, na.remove=TRUE){ l.max <- max(data, na.rm=na.remove) l.i <- match(l.max, data) c(max=l.max, i=l.i) } f.maxi(c(3,4,78,2)) apply(d.sport, 2, f.maxi) ## if f.log <- function(x){ if (any(x<=0)) { stop("Zahlen sind unbrauchbar.") } else { l.x <- log(x) return(l.x) } } x <- 1 for (i in 1:10){ x <- x*(x+1) if (x>1000000) break } ## Fehlermeldungen traceback()} f.maxi <- function(data, na.remove=TRUE){ on.exit(browser()) l.max <- max(data, na.rm=na.remove) l.i <- match(l.max, data) on.exit() c(max=l.max, i=l.i) } debug(f.maxi) ## Kapitel Objekte ## Listen t.l <- hist(t.kugel,plot=FALSE) t.l ## Komponenten einer Liste auswählen t.l[2:3] t.l[c("breaks","intensities")] t.l["counts"] t.l[[2]] t.l[["counts"]] t.l$counts ## data.frame ## Matrizen t.m <- matrix(1:15, nrow=3, ncol=5) t.m[2,1:3] cbind(4:6,13:15) ## Eigenschaften von Objekten mode(t.kugel) is.matrix(d.sport) is.data.frame(d.sport) attributes(d.sport) str(t.l) t.sport.mat <- as.matrix(d.sport) unlist(list(a=1:2, b=5:7)) ## Objektorientiertes Programmieren t.tt <- t.test(t.kugel[t.hh],t.kugel[!t.hh]) class(t.tt) names(t.tt) str(tt) print(t.tt) plot(t.tt) summary(t.tt) ## libraries library(MASS) help.search("Wilcoxon") ## http://www.r-project.org/ ## Weitere Informationene über ein Package library(help=MASS)