### ----------------------------------------------------------------------- ### Autor: Paul Fink, et. al. (Stand: SoSe 2017) ### ----------------------------------------------------------------------- ## ----numeric1------------------------------------------------------------ numvec <- c(2.54, 4.22, 2.99, 3.14, 3.44) numvec ## ----char---------------------------------------------------------------- charvec <- c("Statistische", "Software") charvec ## ----logical------------------------------------------------------------- logicvec <- c(TRUE, FALSE, FALSE, TRUE) logicvec ## ----vector-type--------------------------------------------------------- is.numeric(numvec) is.character(numvec) ## ----structure-object---------------------------------------------------- str(charvec) ## ----automatic-type-conversion------------------------------------------- TRUE + 2 c("Hallo", sqrt(3)) c(2, "Hallo", TRUE) ## ----forced-type-conversion---------------------------------------------- as.numeric(logicvec) as.logical(c(0, pi)) as.character(c(1, 2, 4, TRUE)) as.numeric(charvec) ## ----seq1---------------------------------------------------------------- seq(from = 3, to = -2, by = -0.5) ## ----seq2---------------------------------------------------------------- 2:4 # entspricht seq(from = 2, to = 4) seq(from = 4, to = 2) ## ----seq3---------------------------------------------------------------- seq(to = 10, length = 10, by = 0.5) ## ----rep1---------------------------------------------------------------- rep(3.5, times = 10) rep(1:4, times = 2) ## ----rep2---------------------------------------------------------------- rep(1:4, each = 2) rep(1:4, each = 2, times = 3) ## ----factor-------------------------------------------------------------- x <- factor(c("Saft", "Saft", "Limonade", "Saft", "Wasser"), levels = c("Saft", "Wasser", "Limonade")) x levels(x) ## ----asfactor------------------------------------------------------------ x <- c("Apfel", "Birne", "Apfel", "Traube", "Traube", "Kiwi") x <- as.factor(x) x ## ----numericoperators-vector--------------------------------------------- x <- 1:4 y <- c(4,10,2,0) x + y x * y ## ----logicalops-vector--------------------------------------------------- x < y ## ----recycling1---------------------------------------------------------- x x + c(1, 2) ## ----recycling2---------------------------------------------------------- x + c(1, 2, 1, 2) ## ----recycling3---------------------------------------------------------- x + c(1, 2, 4) # x + c(1, 2, 4, 1) ## ----zugriff-positiv----------------------------------------------------- x <- 1:10 x[1:3] x[c(2, 4, 6)] ## ----zugriff-negativ----------------------------------------------------- x[-(1:5)] ## ----zugriff-leer-------------------------------------------------------- x[] ## ----zugriff-logisch----------------------------------------------------- x[(x > 5)] x[((x %% 2) == 0)] ## ----zugriff-name-------------------------------------------------------- xn <- c(Wasser = 1, Saft = 2, Limonade = 3 ) names(xn) xn["Saft"]