## ------------------------------------------------------------------- ## Befehle zur Kurzeinfuehrung ## --------------------------- ## Zuweisung, print t.v <- c(2,-1,9,0.4,100) t.v ##- [1] 2.0 -1.0 9.0 0.4 100.0 ## Hilfe ?c help.search("princomp") help.start() ## Objekte: Vektoren, Matrizen, Listen rep(0,10) [1] 0 0 0 0 0 0 0 0 0 0 rep(t.v,3) ##- [1] 2.0 -1.0 9.0 0.4 100.0 2.0 -1.0 9.0 0.4 100.0 2.0 -1.0 ##- [13] 9.0 0.4 100.0 t.vs <- seq(0,2,0.5) t.i <- 1:7 t.m <- matrix(t.v,5,3) t.m <- cbind(1:5,t.v,c(0,0,2,2,3)) t.m list(t.v,t.m) mode(t.m) as.character(1:3) as.logical(c(0,1,-1,3,0.5)) ## Bezeichnungen pi pi <- 2 pi c c <- 5 c(3,1) ## ------------------------------------------------------------------- ## Funktionen matrix(t.v, nrow=5, ncol=3) matrix(t.v, ncol=3) matrix(t.v, nrow=5, byrow=TRUE) diag(1:3) diag(t.m) ## Operationen c(2, 4)*c(3, -1) c(2, 4)*3 # recycling t.v*c(2,0) 1:5+t.m t.m-t.v # dirty trick! 1/0 0/0 ## mathematische Funktionen log(c(1,10,0,-2,NA)) min(t.v, 500, 1:10) which.min(t.v) pmin(t.v,1:5) ## Matrix-Multiplikation t.m %*% c(2,1,0) t.v %*% t.m t.m %*% t.v ## Logische Operatoren, Vergleiche t.v>1 t.l <- t.v>1 & t.v<=10 t.l t.v*(t.v>1) # Umwandlung, Prioritaet ## Zusammenfassende Funktionen sum(t.v) median(t.v) apply(t.m,2,mean) # keine loops! ## ------------------------------------------------------------------- ## Namen und Element-Auswahl ## Elements mit numerischen Indices t.v[2:3] t.m[c(3,5),3] t.m[,3] ## Namen names(t.v) <- c("Hans","Fritz","Elsa","Trudi","Olga") t.v["Elsa"] t.vn <- names(t.v) dimnames(t.m) <- list(t.vn, c("V1","V2","V3")) t.m[c("Elsa","Fritz"),"V1"] t.m["Elsa",1] ## direkte Zuweisung von Namen t.v <- c(Hans=2,Fritz=-1,Elsa=9,Trudi=0.4,Olga=100) t.m <- cbind(V1=1:5, V2=t.v, V3=c(0,0,2,2,3)) ## paste paste("V", 1:3, sep=".") paste("AB","CD","ef") dimnames(t.m)[[2]] <- paste("V", 1:3, sep="") ## ------------------------------------------------------------------- ## Daten ## d.abst <- read.table("http://stat.ethz.ch/~stahel/data/abst.dat",header=TRUE) data(iris) iris[25*1:6,] t.x <- as.matrix(iris[,1:4]) # Umwandlung in eine Matrix ## ------------------------------------------------------------------- ## Grafik plot(iris[,1],iris[,2],xlab="Sepal.Length",ylab="Sepal.Width",main="Iris") ## dev.print() sunflowerplot(iris[,1],iris[,2], xlab="Sepal.Length",ylab="Sepal.Width",main="Iris") plot(c(iris[,1],50),c(iris[,2],10), xlab="Sepal.Length",ylab="Sepal.Width",main="Iris") pairs(iris,main="Iris") ## ------------------------------------------------------------------- ## "Frames" oder "environments" ## ------------------------------------------------------------------- ## Verteilungen und Zufallszahlen dnorm(7,5,2) # Dichte der Normalverteilung für x=7, mu=5, sigma=2. pnorm(7,5,2) # P(X <= 7) qnorm(c(0.025, 0.5, 0.975)) # Quantile der Standard-Normalverteilung rnorm(10)} # Zufallszahlen qchisq(0.95,2) pchisq(5.8, df=1, lower.tail=FALSE) set.seed(28) runif(10) sample(8) # zufaellige Reihenfolge sample(3, size=10, replace=TRUE) # Zufallsauswahl mit Zuruecklegen