-
Notifications
You must be signed in to change notification settings - Fork 23
/
Monday2.R
165 lines (125 loc) · 3.12 KB
/
Monday2.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Date: Monday, August 6, 2012
###########################
### Arrays and Patterns ###
### (and some plots) ###
###########################
# There are various possibilities to create
# one-dimensional arrays (vectors):
#
# If x is an array, x[j] is its j-th component.
# length(x) is the number of its components.
# The simplest command to generate an array is
# the command c(ombine)(...):
x <- c(1.8, 6, 7.5)
x
# Careful, the first element is indeed number 1, not 0
x[1]
x[2]
x[3]
length(x)
y <- c(x, 30, 1000)
y
y[3:5]
length(y)
# Creation of arrays with regular patterns:
# For integers a <= b,
# x <- a:b
# generates an array x with components
# a, a+1, a+2, ..., b.
# In case of a > b, one obtains a vector with components
# a, a-1, a-2, ..., b.
x <- -5:5
x
x <- 10:0
x
# To obtain different increments, use seq(...),
# seq standing for "sequence":
x <- seq(0,10,by=0.1)
x
x <- seq(0,10,by=0.7)
x
x <- seq(0,10,length.out=201)
x
x <- seq(0,10,length.out=77)
x
# Create a vector y with components
# y[i] = sin(x[i]):
y <- sin(x)
y
# Visualize the two vectors:
plot(x,y)
plot(y) #Just numbers the components, using this as x axis.
# p(oint) ch(aracter):
plot(x,y,pch="+")
plot(x,y,pch=16)
plot(x,y,pch=25)
# Obviously, the procedure plot(.) has
# many options. To find out more about it:
? plot
# Line plots:
plot(x,y,type="l")
plot(x,y,type="l",lty=2)
plot(x,y,type="l",lty=3)
plot(x,y,type="l",lty=1,col="red")
plot(x,y,type="l",lty=1,col="red",lwd=5)
# plot(..) always starts a new plot,
# i.e. overwrites an existing plot.
# To add further plots, use points()
# or lines()
plot(x,y,type="l",lty=1,col="red",lwd=5)
points(x,cos(x),type="l",col="blue")
lines(x,cos(x-0.1*pi),col="magenta")
points(x,cos(x-0.2*pi),pch=4,col="cyan")
# Help files:
#
# help.search("key word(s)"):
# Useful, if one has only a vague idea.
#
# help("name_of_fct")
# or help(name_of_fct)
# or ? name_of_fct :
# Useful if one knows that there is a function
# called name_of_fct
help.search("sequence")
help("seq")
help(seq)
?seq
# Building up larger arrays from smaller ones:
# rep(...) !
?rep
# Try to generate with rep() rather than c(...):
x = c(1,2,3, 1,2,3, 1,2,3, 1,2,3, 1,2,3, 1,2,3, 1,2,3)
x
y = c(1,1,1,1, 2,2,2,2, 3,3,3,3)
y
z = c(1,1,1,1,1,1,1, 2,2,2,2,2, 3,3,3, 4)
z
rep(1:4, times=c(7,5,3,1))
# Try yourself - don't read on yet...
# Solutions:
rep(1:3, 7)
rep(1:3, times=7)
rep(1:3, each=4)
rep(1:4, times=c(7,5,3,1))
rep(c(1,2,3,4), times=c(7,5,3,1))
rep(1:4, times=seq(7,1,by=-2))
# Note that the usage of "times" and "each" is not
# entirely coherent...
# There are arrays of other objects,
# e.g. character strings:
x <- c("Frieda","Fritz","Frederik")
rep(x, times= 4)
rep(x, each=5)
rep(x, times=c(4, 1, 2))
rep(c("misch", "fa", "lea"), times=c(100, 50,23))
x = seq(0,1, length.out=10000)
y = x*sin(2*pi/x)
y[1] = 0
plot(x, y, type="l", ylab="x*sin(2*pi/x)", main="Mein erster Plot")
lines(x,x, col="Cyan")
lines(x,-x, col="Cyan")
#version number 2, higher resolution the closer to 0!
# also doesn't create any nans
x <- 1/seq(500,1,-0.02)
y <- x*sin(2*pi/x)
plot(c(0,x),c(0,y), type = "l", ylab="x*sin(2*pi/x)", main="Lösungsplot")