########################################## ## Grundrechenarten mit R ## ########################################## 2+5*7 5/3+2 5/(3+2) 5/3^2 (5/3)^2 5^18 5/0 0/0 ##NaN: Not a Number ######################################### ## Zuweisungen ## ######################################### # Zuweisungsoperator '<-' x <- 7 # Werte von Variablen am Bildschirm ausgeben: print(x) x ######################################### ## Datentypen/Attribute ## ######################################### # Numerische Datentypen x <- 7 length(x) mode(x) is.integer(x) is.double(x) # Logische Variablen y <- is.numeric(x) y # Character z <- "A" z z <- 'test' z ######################################### ## Working Space ## ######################################### name <- "Knut"; n1 <- 10; n2 <- 100; m <- 0.5 ls() ls.str() save(x, name, m, file="somedata.RData") rm(name, n1) ls() load("somedata.RData") ls() ## working directory getwd() setwd("c:\\temp") ######################################### ## Vektoren ## ######################################### # Kombinieren von Elementen x <- c(5, 2, 8, 99) x x <- c(x, 3, 4) x y <- c("A", "B") y z <- c(TRUE,FALSE,TRUE) xz<-c(x,z) # implizite Typkonversion xz as.numeric(z) # explizite Typkonversion # Bestimmung von Typ und Länge mode(z) mode(x) is.numeric(x) is.logical(x) is.character(y) length(z) # Sequenzen seq(from=0, to=10, by=2) seq(from=0, to=10, length=5) seq(to=10) 1:10 # Wiederholungen rep(0, times=5) rep(1:3, times=5) rep(1:3, length=5) rep(1:3, each=5) # Zugriff auf Elemente x <- 2:7 x x[2] x[-3] x[2:4] x[x > 3] x[c(1, 3:5)] x[2:4] <- 10 x # Rechnen mit Vektoren x <- 1:3 y <- 3:1 x+1 x+y sum(x) cumsum(x) prod(y) cumprod(y) x*y exp(x) x^2 # Fehlende Werte (Not Available) x <- c(7, NA, 3, 5) max(x) max(x, na.rm=TRUE) ######################################### ## Matrizen ## ######################################### # Erstellen von Matrizen m <- matrix(1:12, ncol=4, nrow=3) m m <- matrix(m, ncol=4, nrow=3, byrow=T) m # Zugriff auf Elemente m[1,2] m[,1] m[2,] m[2:3, -4] # Typ und Dimension mode(m) length(m) dim(m) attributes(m) rownames(m) <- c("aber", "oder", "und") attributes(m) m # Kombinieren von Vektoren und Matrizen x <- 1:3 y <- 3:1 m <- cbind(x, y) m m <- rbind(x, y) m # Transponieren & Matrixmultiplikation t(m) m2 <- m %*% t(m) m2 x <- rep(1, 2) m2 %*% x x %*% m2 ########################################## ## Listen ## ########################################## # Erstellen von Listen div <- list(vector=1:5, char="ABC", mat=matrix(1:4, ncol=2, nrow=2)) div str(div) attributes(div) # Zugriff auf Elemente div$char div[[2]] div[[1]][-1] ########################################## ## Daten lesen und schreiben ## ########################################## #daten <- read.table("http://www.statistik.lmu.de/institut/lehrstuhl/semwiso/lebensdaueranalyse/download/bmt.dat", header=TRUE) daten <- read.table("bmt.dat", header=TRUE) names(daten) str(daten) dim(daten) daten[1:2,] daten$deathtime[1:10] daten2 <- edit(daten) all.equal(daten, daten2) write.table(daten2, file="bmt2.dat") ########################################## ## Funktionen/Packages ## ########################################## # install.packages("survival") library("splines") search() sessionInfo() detach(package:splines) search() attach(daten) group ########################################## ## Hilfefunktionen ## ########################################## help(Syntax) ?"+" help("bs", try.all.packages=TRUE) help("bs", package = "splines") apropos(help) #options()$browser #options(browser="L:/FireFox10/firefox.exe") help.start() ######################################### ## Programmierung ## ######################################### # Funktionen square <- function(x) { return(x*x) } square(3) pow <- function(x, power) { return(x^power) } pow(2, 2) pow(power=3, x=2) # for-Schleifen forfun1 <- function(x=rep(0, 10)) { for(i in seq(along=x)) { x[i] <- x[i] + i } return(x) } forfun1() forfun1(5:3) forfun2 <- function(x=rep(0, 10)) { for(i in seq(2, 10, 2)) { x[i] <- x[i] + i } invisible(x) } forfun2() nx <- forfun2() nx # while-Schleifen x <- 1 while( x < 10 ) { x <- 2*x print(x) } # if-Abfragen mybernoulli <- function(prob=0.5) { u <- runif(1) if (u < prob) return(1) else return(0) } mybernoulli() ##entspricht rbinom(1, 1, 0.5) ######################################## ## Regressions- und Formelobjekte ## ######################################## x <- runif(100, 0, 5) y <- 0.5*x + rnorm(100, sd=0.5) formel <- y ~ x formel lm1 <- lm(formel) lm1 is.list(lm1) str(lm1) attributes(lm1) lm1$coefficients coef(lm1) summary(lm1) yhat <- lm1$fitted.values yhat.test <- lm1$coef[1] + lm1$coef[2]*x identical(yhat, yhat.test) range(yhat - yhat.test) all.equal(yhat, yhat.test) names(yhat)<-NULL all.equal(yhat, yhat.test) ######################################### ## Grafiken ## ######################################### # Erzeugen von Grafiken v <- runif(100, 0, 2*pi) w <- sin(v) plot(v, w) windows() plot(v, w, type="l") o <- order(v) v <- v[o] w <- w[o] plot(v, w, type="l") plot(v, w, type="s") # Ergänzen von Grafiken points(v, cos(v), pch=19) lines(v, cos(v), col="red", lwd=2) # mehrere Grafiken in einem Bild if (.Platform$OS.type == "unix") x11() else windows() par(mfrow=c(2, 2)) plot(v, w, type="l") plot(v, w, type="s") plot(v, cos(v), type="l") plot(v, cos(v), type="s") graphics.off() # Regressionsbeispiel plot(x, y, ylim=range(y, yhat), ylab="", main="Regression Example") par(new=TRUE) plot(x, yhat, type="o", col="blue", ylim=range(y, yhat), ylab="") legend(4, 0, c("y", expression(hat(y))), col=c("black", "blue"), pch=c(19,19)) plot(lm1) ?plot.lm # Speichern im PDF-Format pdf(file="sinus.pdf", width=5, height=5, paper="special", pointsize=12) plot(v, w, type="l") dev.off()