Dateianhang 'solutions.r'
Herunterladen 1 ## 1
2 # 33. Try to use R as a calculator: What is greater exp(pi) oder pi^e.
3 exp(pi) > pi**exp(1)
4
5 max(c(exp(pi),pi**exp(1)))
6
7 which.max(c(exp(pi),pi**exp(1)))
8
9
10
11
12
13
14
15
16
17
18 ## 3
19 # 1 Create a vector w with components 1, -1, 2, -2
20 (w <- c(1,-1,2,-2)) # or
21 (w <- rep(c(1,2),c(2,2)) * c(1,-1)) ## just interesting if you want to create a vector like this
22 # 2
23 w
24 # 3
25 str(w)
26 # 4
27 w + 1
28 # 5
29 v <- c(0,1,seq(5,75,by=5))
30 # 6
31 length(v)
32
33 ## 3
34 x <- c(2, 7, 0, 9, 10, 23, 11, 4, 7, 8, 6, 0)
35 x[4]
36 x[3:5]
37 x[c(1, 5, 8)]
38
39 x > 10
40 i <- x > 10
41 x[x > 10]
42
43 x[i]
44
45 i <- (1:6) * 2
46 x[(1:6) * 2]
47 x[i]
48
49 x == 0
50 x[x == 0] <- 1
51 x
52
53 round(x/2) == x/2
54
55 ifelse(round(x/2) == x/2, "even", "odd")
56
57 # 3 part 2
58 # using a numeric index
59 (i <- seq(3,length(x),by=3))
60 (i <- c(3,6,9,12))
61 (i <- (1:4)*3)
62
63 x[seq(3,length(x),by=3)]
64 x[i]
65
66 # using logic index
67 (i <- c(1:length(x)) %% 3 == 0)
68 x[i]
69
70 # 2
71 (i <- x > 4 & x < 10)
72 x[i]
73
74 # 3
75 (i <- x > 10)
76 x[i] <- 10
77 x ## or shorter
78 x[x > 10] <- 10
79
80 # 4
81 (i <- x < 5)
82 x[i] <- x[i]*2
83
84 x[x < 5] <- x[x < 5]*2
85
86 x <- ifelse(x < 5,x*2,x)
87
88 # 5
89 y <- rep(0:1,6)
90
91 (z1 <- ifelse(y==0,x,3*x))
92
93 (z2 <- x+x*y*2)
94
95 (z3 <- x*(1+y*2))
96
97
98 ### 3.2
99 load("presidential.rdata")
100 presidential
101
102 ## 1
103 presidential[presidential$party=="Republican",]
104
105 ## 2
106 presidential[presidential$party=="Democratic",]
107
108 ## 3
109 presidential[presidential$duration > 3 & presidential$duration < 6,]
110
111 ## 4
112 #### table
113 table(presidential$party)
114
115 ### true of duration max (8)
116 presidential$durmax <- presidential$duration==8
117
118 #### table party - durmax
119 table(presidential$party,presidential$durmax)
120
121
122 ####
123 load("201404zeiten.rdata")
124
125 head(t098)
126 names(t098)
127 summary(t098)
128
129
130 t098$dauer <- t098$ENDE - t098$BEGINN
131
132 t098$dauer <- as.numeric(t098$ENDE - t098$BEGINN)/60
133
134 t098$dauer[t098$dauer > 60] <- NA
135
136
137 summary(t098)
138
139 hist(t098$dauer)
140
Gespeicherte Dateianhänge
Um Dateianhänge in eine Seite einzufügen sollte unbedingt eine Angabe wie attachment:dateiname benutzt werden, wie sie auch in der folgenden Liste der Dateien erscheint. Es sollte niemals die URL des Verweises ("laden") kopiert werden, da sich diese jederzeit ändern kann und damit der Verweis auf die Datei brechen würde.Sie dürfen keine Anhänge an diese Seite anhängen!