############################################################################### ## Inhalt: ## Wichtige Objekte ## vgl. Abschnitt 1.2 im Skript von Ruckdeschel und Kohl ############################################################################### x1<-25:30 .Last.value b<-a<-6 a b b<-5 b->a b b<-4 b_a length(a) mode(a) ## Bsp 1.2-1 a<-seq(1,6,1) ## fuellt a mit 1,..,6 ##(siehe auch Abschnitt`~\hyperlink{asec1.3.1}{\tt 1.3.1}`) length(a) ## Laenge von a mode(a) ## Modus von a a[3] ## gibt Element Nr. 3 von a aus a[4]<-3 ## setzt Element Nr. 4 auf 3 names(a)<-c('a','b','c','d','e','f') ## benennt die Elemente als a ... f a names(a) a["d"] a<-matrix(1:15,5,3) a[4,2] a[4,] a<-array(1:60,dim=c(5,3,4)) a[4,2,3] a[,2,] ## Array oder Matrix ist nichts anderes als ein Vektror ## mit einem 'dim' Argument. Also (x <- c(1:12)) ## ist Vektor dim(x) <- c(3, 4) ## ist Matrix mit 3 Zeilen und 4 Spalten x dim(x) <- c(3, 2, 2) ## Array der Dimension 3x2x2 x ae3<-"Hallo" del<-0.1 A<-list(a=ae3,del=del) A1<-c(ae3,del) A[[2]] A$del A$d unlist(A) ## Beispiel 1.2-2 ############################################ ## Teil (a) ############################################ v <- 1:30 ## oder: v <- seq(from=1, to=30, by=1) ## oder: v <- seq(form=1, to=30, length=30) ## oder (umstaendlich, aufwendig): v <- vector("numeric", length=30); for(i in 1:30) {v[i] <- i} ############################################ ## Teil (b) ############################################ v[v>=20] <- 20 v[v<=5] <- 5 ############################################ ## Teil (c) ############################################ M <- matrix(data=v, nrow=3, ncol=10) M[,2] ############################################ ## Teil (d) ############################################ A <- array(data=v, dim=c(2,3,5)) A[,,3] ############################################ ## Teil (e) ############################################ L <- list(v=v,M=M,A=A) rm(v,M,A) L[[2]][2,3] ##bzw. L$M[2,3] bg<-factor(c("D","N","B","D","E","D","F","I")) bg print.default(bg) bg<-factor(c("a","b"),levels=c("a","b","c")) bg ek<-ordered(c("h","t","m","t"));ik<-ordered(c("h","m","t")) ek a<-cbind(matrix(1:15,5,3),matrix(1,ncol=1,nrow=5)) a a<-rbind(matrix(1:15,5,3),matrix(2,nrow=1,ncol=4)) a ## Bsp 1.2-2 library(MASS) ## laedt die MASS-library data(painters) ## laedt den Datensatz "painters" ##(siehe auch Abschnitt`~\hyperlink{asec1.3.1}{\tt 1.3.2}`) painters ## gibt ihn aus row.names(painters) ## Zeilennamen (Malernamen) painters[1:5, c(2,5)] ## spezielle Auswahl ## Bsp 1.3-1 x<- 1:4 ## c(1,2,3,4)->x i<-rep(2,4) ## c(2,2,2,2)->i y<-rep(x,2) ## c(1,2,3,4,1,2,3,4)->y z<-rep(x,i) ## c(1,1,2,2,3,3,4,4)->z w<-rep(x,x) ## c(1,2,2,3,3,3,4,4,4,4)->w ######################################### ## 2way design ## 4 Zeilenklassen ## 3 Spaltenklassen ## jeweils 2 Beobachtungen ######################################### colc<-rep(1:3,rep(8,3)) ## ergibt: ## 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 rowc<-rep(rep(1:4,rep(2,4)),3) ## ergibt: ## 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4 a1<-"" a2<-character(0) length(a1) length(a2) nchar(c("Hallo Rudie")) nchar(c("Hallo ", "Rudie")) paste(c("Hallo", " Welt"), 1:3, 2==1+1) ## Bsp 1.5-1 data(painters,package=MASS) ## mittlerweile klar pnames<-row.names(painters) ## Namen der Maler in pnames substring(pnames[1:4],1,5) as.vector(abbreviate(pnames[1:4])) x<-round(rnorm(40))/round(rnorm(40)) y<-round(rnorm(40)) z<-(x+y)[!is.na(x) & x>0] z x[c(1:3,5,10:13,5:1)] x[-(1:3)] ## Bsp 1.6-1 obst<-c(5,10,1) names(fruit)<-c("Orange","Birne","Apfel") essen<-obst[c("Apfel","Orange")] essen a<-array(1:120,dim=c(3,4,10)) dimnames(a)<-list(letters[1:3], c("i","ii","iii","iv"),NULL) a aperm(a,(2,3,1)) a ## Bsp 1.7-1 ## X~Bin(4,0.3) und Y~Bin(3,0.1) unabh. ## gesucht: die Wahrsch.fkt. von X+Y x<-dbinom(0:4,size=4,p=0.3) ## dbinom(..)=P(X=x) y<-dbinom(0:3,size=3,p=0.1) ## dbinom(..)=P(Y=y) xy<-x%o%y ## ergibt pi*qj ind<-outer(0:4,0:3,"+") pf<-function(i){ ind0<-(ind==i) return(sum(ind0*xy))} c(pf(0),pf(1),pf(2),pf(3),pf(4),pf(5),pf(6),pf(7)) ## Bsp 1.7-2 data(iris3) ## laden des IRIS- dim(iris3) ## Datensatzes help(iris3) ## Info dazu ir.means<-apply(iris3, c(2,3), mean) ## Anwendung von mean auf jeden ## j,k schnitt von iris[i,j,k] apply(iris3, c(2,3), mean, trim=0.1) ## zus. Arg von mean -> getrimmtes Mittel apply(iris3, c(2), mean) ir.var<-apply(iris3,3,var) ## Varianz eines jeden k-Schnitts sweep(iris3,c(2,3),ir.means) ######### von Venables Ripley ##-*- R -*- ## Chapter 2 The S Language ## 2.1 A concise description of S objects mydata <- c(2.9, 3.4, 3.4, 3.7, 3.7, 2.8, 2.8, 2.5, 2.4, 2.4) colours <- c("red", "green", "blue", "white", "black") x1 <- 25:30 x1 mydata[7] colours[3] mydata > 3 names(mydata) <- c('a','b','c','d','e','f','g','h','i','j') mydata names(mydata) mydata["e"] letters[1:5] mydata[letters[1:5]] mydata[mydata > 3] mydata[-c(3:5)] mode(mydata) mode(letters) mode(sin) length(mydata) length(letters) length(sin) names(mydata) <- NULL ## remove the names dim(mydata) <- c(2, 5) mydata dim(mydata) <- NULL matrix(mydata, 2, 5) matrix(mydata, 2, 5, byrow=T) Empl <- list(employee="Anna", spouse="Fred", children=3, child.ages=c(4,7,9)) Empl$employee Empl$child.ages[2] names(Empl) <- letters[1:4] Empl[3:4] Empl <- c(Empl, service = 8) unlist(Empl) unlist(Empl, use.names=F) c(list(x = 1:3, a = 3:6), list(y = 8:23, b = c(3, 8, 39))) c(list(x = 1:3, a = 3:6), list(y = 8:23, b = c(3, 8, 39)), recursive=T) citizen <- factor(c("uk","us","no","au","uk","us","us")) citizen print.default(citizen) unclass(citizen) citizen <- factor(c("uk","us","no","au","uk","us","us"), levels = c("us", "fr", "no", "au", "uk")) citizen table(citizen) ## kommt noch income <- ordered(c("Mid","Hi","Lo","Mid","Lo","Hi","Lo")) income as.numeric(income) inc <- ordered(c("Mid","Hi","Lo","Mid","Lo","Hi","Lo"), levels = c("Lo", "Mid", "Hi")) inc data(geyser) erupt <- cut(geyser$duration, breaks = 0:6) ## cut nicht behandelt erupt <- ordered(erupt, labels=levels(erupt)) erupt data(painters) painters row.names(painters) painters[1:5, c(2, 4)] ## 2.2 Calling conventions for functions args(hist) ## 2.3 Arithmetical expressions x <- c(10.4, 5.6, 3.1, 6.4, 21.7) y <- c(x, x) v <- 2 * x + y + 1 xtrunc <- pmax(0, pmin(1,x)) s3 <- seq(-5, 5, by=0.2) s3 s4 <- seq(length=51, from=-5, by=0.2) s4 s5 <- rep(x, times=5) s5 x <- 1:4 ## puts c(1,2,3,4) into x x i <- rep(2, 4) ## puts c(2,2,2,2) into i i y <- rep(x, 2) ## puts c(1,2,3,4,1,2,3,4) into y y z <- rep(x, i) ## puts c(1,1,2,2,3,3,4,4) into z z w <- rep(x, x) ## puts c(1,2,2,3,3,3,4,4,4,4) into w w colc <- rep(1:3,rep(8,3)); colc rowc <- rep(rep(1:4,rep(2,4)), 3); rowc 1 + (ceiling(1:24/8) - 1) %% 3 -> colc; colc 1 + (ceiling(1:24/2) - 1) %% 4 -> rowc; rowc ## 2.6 Character vector operations paste(c("X","Y"), 1:4) paste(c("X","Y"), 1:4, sep="") paste(c("X","Y"), 1:4, sep="", collapse=" + ") data(state) substring(state.name[44:50], 1, 4) ## 2.8 Indexing vectors, matrices and arrays letters[1:3] letters[1:3][c(1:3,3:1)] longitude <- state.center[["x"]] names(longitude) <- state.name longitude[c("Hawaii", "Alaska")] a <- 1:4 a[0] a[0] <- 10 a mydata sort(mydata) x <- rnorm(100001) sort(x, partial=50001)[50001] latitude <- state.center[["y"]] names(latitude) <- state.name i <- sort.list(longitude) cbind(latitude = latitude[i], longitude = longitude[i]) data(shoes) shoes$B rank(shoes$B) rank(round(shoes$B)) sort.list(sort.list(round(shoes$B))) ##if(version$major >= 5) showConnections(all=T) ## End of ch02