finish aufgabe 1 testat 1
parent
dcf0d99797
commit
1ae25adad6
|
|
@ -0,0 +1,423 @@
|
|||
do.call(sqrt,4)
|
||||
do.call(sqrt,list(4))
|
||||
pi |> sqrt
|
||||
pi |> sqrt()
|
||||
l1 = seq(0,2*pi,length=100)
|
||||
l1.length
|
||||
l1.length()
|
||||
l1$length
|
||||
l1
|
||||
length(l1)
|
||||
plot(l1,)
|
||||
sin = sin(x)
|
||||
sin = sin(l1)
|
||||
plot(x,sin, type="l")
|
||||
plot(l1,sin, type="l")
|
||||
carsten = sin(l1)
|
||||
plot(carsten,sin, type="l")
|
||||
plot(l1,carsten, type="l")
|
||||
?rep
|
||||
# Ihre Lösung
|
||||
data.frame(seq(0:10))
|
||||
# Ihre Lösung
|
||||
data.frame(seq(0:10), seq(3,4))
|
||||
# Ihre Lösung
|
||||
data.frame(seq(0:10), seq(0:10))
|
||||
wort = rep("Wort",100)
|
||||
data.frame(zahl,wort)
|
||||
df.head()
|
||||
df <- data.frame(zahl,wort)
|
||||
# Ihre Lösung
|
||||
zahl <- seq(5,25,length=100)
|
||||
hl <- seq(5,25,length=100)
|
||||
wort <- rep("Wort",100)
|
||||
df <- data.frame(zahl,wort)
|
||||
df.head()
|
||||
head(df)
|
||||
head(df,10)
|
||||
res = df[[67]]
|
||||
res = df
|
||||
df
|
||||
res = df$zahl
|
||||
res
|
||||
res = df$zahl[[67]]
|
||||
res
|
||||
print(round(res,2))
|
||||
# Ihre Lösung:
|
||||
M
|
||||
set.seed(12) # Zufallszahlengenerator gleich machen.
|
||||
M = matrix(sample(c(1, 2, 3), 10 * 100, T), nrow = 100)
|
||||
# Ihre Lösung:
|
||||
M
|
||||
?apply
|
||||
# Ihre Lösung:
|
||||
apply(M,sum)
|
||||
```{r}
|
||||
# Ihre Lösung:
|
||||
apply(M,M[:,2],sum)
|
||||
# Ihre Lösung:
|
||||
apply(M,M[1],sum)
|
||||
# Ihre Lösung:
|
||||
apply(M,M[2],sum)
|
||||
# Ihre Lösung:
|
||||
apply(M,M[3],sum)
|
||||
# Ihre Lösung:
|
||||
apply(M,M[1],sum)
|
||||
# Ihre Lösung:
|
||||
apply(M,M[2],sum)
|
||||
# Ihre Lösung:
|
||||
apply(M,M[2],sum)
|
||||
# Ihre Lösung:
|
||||
apply(M,M[1],sum)
|
||||
# Ihre Lösung:
|
||||
apply(M,M[1],sum)
|
||||
# Ihre Lösung:
|
||||
apply(M,c(1,2),sum)
|
||||
apply(M,c(1,2),sum)
|
||||
apply(M,2,sum)
|
||||
k = M[:,5]
|
||||
k = M[,5]
|
||||
k
|
||||
length(k)
|
||||
sum(k)
|
||||
k = M[,1]
|
||||
k
|
||||
set.seed(12) # Zufallszahlengenerator gleich machen.
|
||||
M = matrix(sample(c(1, 2, 3), 10 * 100, T), nrow = 100)
|
||||
# Ihre Lösung:
|
||||
apply(M,2,sum)
|
||||
k = M[,1]
|
||||
sum(k)
|
||||
apply(M,2,sqrt)
|
||||
0
|
||||
set.seed(12) # Zufallszahlengenerator gleich machen.
|
||||
M = matrix(sample(c(1, 2, 3), 10 * 100, T), nrow = 100)
|
||||
# Ihre Lösung:
|
||||
count_2 = function(a){
|
||||
if (a == 2){
|
||||
return 1
|
||||
1
|
||||
set.seed(12) # Zufallszahlengenerator gleich machen.
|
||||
M = matrix(sample(c(1, 2, 3), 10 * 100, T), nrow = 100)
|
||||
# Ihre Lösung:
|
||||
count_2 = function(a){
|
||||
if (a == 2){
|
||||
1
|
||||
}
|
||||
0
|
||||
}
|
||||
apply(M,2,sum)
|
||||
apply(M,2,count_2)
|
||||
?sqrt
|
||||
count_2(3)
|
||||
count_2(2)
|
||||
0
|
||||
set.seed(12) # Zufallszahlengenerator gleich machen.
|
||||
M = matrix(sample(c(1, 2, 3), 10 * 100, T), nrow = 100)
|
||||
# Ihre Lösung:
|
||||
count_2 = function(a){
|
||||
if (a == 2){
|
||||
1
|
||||
}
|
||||
else{
|
||||
0
|
||||
}
|
||||
}
|
||||
apply(M,2,count_2)
|
||||
count_2(2)
|
||||
?apply
|
||||
```{r}
|
||||
0
|
||||
?all
|
||||
set.seed(12) # Zufallszahlengenerator gleich machen.
|
||||
M = matrix(sample(c(1, 2, 3), 10 * 100, T), nrow = 100)
|
||||
# Ihre Lösung:
|
||||
count_2 <- function(a){
|
||||
print(typeof(a))
|
||||
}
|
||||
apply(M,2,count_2)
|
||||
count_2(2)
|
||||
set.seed(12) # Zufallszahlengenerator gleich machen.
|
||||
M = matrix(sample(c(1, 2, 3), 10 * 100, T), nrow = 100)
|
||||
# Ihre Lösung:
|
||||
count_2 <- function(a){
|
||||
print(typeof(a))
|
||||
print(length(a))
|
||||
}
|
||||
apply(M,2,count_2)
|
||||
count_2(2)
|
||||
apply(M,2,count_2)
|
||||
sum(a)
|
||||
set.seed(12) # Zufallszahlengenerator gleich machen.
|
||||
M = matrix(sample(c(1, 2, 3), 10 * 100, T), nrow = 100)
|
||||
# Ihre Lösung:
|
||||
count_2 <- function(a){
|
||||
sum(a)
|
||||
}
|
||||
apply(M,2,count_2)
|
||||
a[[a==2]]
|
||||
set.seed(12) # Zufallszahlengenerator gleich machen.
|
||||
M = matrix(sample(c(1, 2, 3), 10 * 100, T), nrow = 100)
|
||||
# Ihre Lösung:
|
||||
count_2 <- function(a){
|
||||
a[[a==2]]
|
||||
}
|
||||
apply(M,2,count_2)
|
||||
set.seed(12) # Zufallszahlengenerator gleich machen.
|
||||
M = matrix(sample(c(1, 2, 3), 10 * 100, T), nrow = 100)
|
||||
# Ihre Lösung:
|
||||
count_2 <- function(a){
|
||||
a
|
||||
}
|
||||
apply(M,2,count_2)
|
||||
set.seed(12) # Zufallszahlengenerator gleich machen.
|
||||
M = matrix(sample(c(1, 2, 3), 10 * 100, T), nrow = 100)
|
||||
# Ihre Lösung:
|
||||
count_2 <- function(a){
|
||||
a
|
||||
}
|
||||
res = apply(M,2,count_2)
|
||||
res
|
||||
?sum
|
||||
?count
|
||||
a==2
|
||||
set.seed(12) # Zufallszahlengenerator gleich machen.
|
||||
M = matrix(sample(c(1, 2, 3), 10 * 100, T), nrow = 100)
|
||||
# Ihre Lösung:
|
||||
count_2 <- function(a){
|
||||
a==2
|
||||
}
|
||||
res = apply(M,2,count_2)
|
||||
res
|
||||
num_2 = apply(M,2,sum)
|
||||
set.seed(12) # Zufallszahlengenerator gleich machen.
|
||||
M = matrix(sample(c(1, 2, 3), 10 * 100, T), nrow = 100)
|
||||
# Ihre Lösung:
|
||||
count_2 <- function(a){
|
||||
a==2
|
||||
}
|
||||
res = apply(M,2,count_2)
|
||||
num_2 = apply(M,2,sum)
|
||||
num_2
|
||||
set.seed(12) # Zufallszahlengenerator gleich machen.
|
||||
M = matrix(sample(c(1, 2, 3), 10 * 100, T), nrow = 100)
|
||||
# Ihre Lösung:
|
||||
count_2 <- function(a){
|
||||
a==2
|
||||
}
|
||||
res = apply(M,2,count_2)
|
||||
num_2 = apply(res,2,sum)
|
||||
num_2
|
||||
2_in_r5 = num2[[5]]
|
||||
set.seed(12) # Zufallszahlengenerator gleich machen.
|
||||
M = matrix(sample(c(1, 2, 3), 10 * 100, T), nrow = 100)
|
||||
# Ihre Lösung:
|
||||
count_2 <- function(a){
|
||||
a==2
|
||||
}
|
||||
res = apply(M,2,count_2)
|
||||
num_2 = apply(res,2,sum)
|
||||
2in_r5 = num2[[5]]
|
||||
set.seed(12) # Zufallszahlengenerator gleich machen.
|
||||
M = matrix(sample(c(1, 2, 3), 10 * 100, T), nrow = 100)
|
||||
# Ihre Lösung:
|
||||
count_2 <- function(a){
|
||||
a==2
|
||||
}
|
||||
res = apply(M,2,count_2)
|
||||
num_2 = apply(res,2,sum)
|
||||
two_in_r5 = num2[[5]]
|
||||
set.seed(12) # Zufallszahlengenerator gleich machen.
|
||||
M = matrix(sample(c(1, 2, 3), 10 * 100, T), nrow = 100)
|
||||
# Ihre Lösung:
|
||||
count_2 <- function(a){
|
||||
a==2
|
||||
}
|
||||
res = apply(M,2,count_2)
|
||||
num_2 = apply(res,2,sum)
|
||||
two_in_r5 = num_2[[5]]
|
||||
set.seed(12) # Zufallszahlengenerator gleich machen.
|
||||
M = matrix(sample(c(1, 2, 3), 10 * 100, T), nrow = 100)
|
||||
# Ihre Lösung:
|
||||
count_2 <- function(a){
|
||||
a==2
|
||||
}
|
||||
res = apply(M,2,count_2)
|
||||
z = apply(res,2,sum)
|
||||
two_in_r5 = z[[5]]
|
||||
print("Es gibt {two_in_r5} ")
|
||||
set.seed(12) # Zufallszahlengenerator gleich machen.
|
||||
M = matrix(sample(c(1, 2, 3), 10 * 100, T), nrow = 100)
|
||||
# Ihre Lösung:
|
||||
count_2 <- function(a){
|
||||
a==2
|
||||
}
|
||||
res = apply(M,2,count_2)
|
||||
z = apply(res,2,sum)
|
||||
two_in_r5 = z[[5]]
|
||||
print(paste0("Es gibt", two_in_r5)
|
||||
set.seed(12) # Zufallszahlengenerator gleich machen.
|
||||
M = matrix(sample(c(1, 2, 3), 10 * 100, T), nrow = 100)
|
||||
# Ihre Lösung:
|
||||
count_2 <- function(a){
|
||||
a==2
|
||||
}
|
||||
res = apply(M,2,count_2)
|
||||
z = apply(res,2,sum)
|
||||
two_in_r5 = z[[5]]
|
||||
print(paste0("Es gibt", two_in_r5))
|
||||
set.seed(12) # Zufallszahlengenerator gleich machen.
|
||||
M = matrix(sample(c(1, 2, 3), 10 * 100, T), nrow = 100)
|
||||
# Ihre Lösung:
|
||||
count_2 <- function(a){
|
||||
a==2
|
||||
}
|
||||
res = apply(M,2,count_2)
|
||||
z = apply(res,2,sum)
|
||||
two_in_r5 = z[[5]]
|
||||
print(paste0("Es gibt ", two_in_r5, " in Spalte 5"))
|
||||
set.seed(12) # Zufallszahlengenerator gleich machen.
|
||||
M = matrix(sample(c(1, 2, 3), 10 * 100, T), nrow = 100)
|
||||
# Ihre Lösung:
|
||||
count_2 <- function(a){
|
||||
a==2
|
||||
}
|
||||
res = apply(M,2,count_2)
|
||||
z = apply(res,2,sum)
|
||||
two_in_r5 = z[[5]]
|
||||
print(paste0("Es gibt ", two_in_r5, " 2er in Spalte 5"))
|
||||
set.seed(12) # Zufallszahlengenerator gleich machen.
|
||||
M = matrix(sample(c(1, 2, 3), 10 * 100, T), nrow = 100)
|
||||
# Ihre Lösung:
|
||||
count_2 <- function(a){
|
||||
sum(a==2)
|
||||
}
|
||||
res = apply(M,2,count_2)
|
||||
z = apply(res,2,sum)
|
||||
res
|
||||
set.seed(12) # Zufallszahlengenerator gleich machen.
|
||||
M = matrix(sample(c(1, 2, 3), 10 * 100, T), nrow = 100)
|
||||
# Ihre Lösung:
|
||||
count_2 <- function(a){
|
||||
sum(a==2)
|
||||
}
|
||||
z = apply(M,2,count_2)
|
||||
print(paste0("Es gibt ", z[[5]], " 2er in Spalte 5"))
|
||||
seq(1, 10, length.out = 20)
|
||||
length(seq(1, 10, length.out = 20))
|
||||
r = seq(1, 10, length = 20)
|
||||
r
|
||||
r = seq(1, 10, by=0.5)
|
||||
r
|
||||
r = seq(0, 10, length = 20)
|
||||
r
|
||||
r = seq(1, 10, length = 21)
|
||||
r
|
||||
r = seq(0, 10, length.out = 20)
|
||||
r
|
||||
r = seq(0, 10, length.out = 10)
|
||||
r
|
||||
r = seq(1, 10, length.out = 10)
|
||||
r
|
||||
r = seq(1, 10, length.out = 20)
|
||||
r
|
||||
setwd("~/Documents/Masterstudium/1.Semester/DSR")
|
||||
git pulll
|
||||
clear()
|
||||
if (min >= max) {
|
||||
stop("min muss kleiner als max sein.")
|
||||
}
|
||||
# Bitte mit echter Lösung in myhistogram.R ersetzen:
|
||||
myhistogram = function(x, n, min, max) {
|
||||
if (missing(min)) min <- base::min(x)
|
||||
if (missing(max)) max <- base::max(x)
|
||||
if (missing(n)) n <- 10
|
||||
if (min >= max) {
|
||||
stop("min muss kleiner als max sein.")
|
||||
}
|
||||
range <- min + max
|
||||
interval_ranges <- (max - min) / n
|
||||
borders <- seq(min, max, length.out = n + 1)
|
||||
counts <- rep(0, n)
|
||||
for(value in x) {
|
||||
for (i in 1:n){
|
||||
if (value >= borders[i] && value < borders[i+1]){
|
||||
counts[i] <- counts[i] + 1
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
print(paste("Intervallgrößen: ", interval_ranges))
|
||||
list(borders=borders, counts = counts)
|
||||
}
|
||||
x = seq(-5, 6, by = 1 / 3)
|
||||
l = myhistogram(x)
|
||||
print(l$borders)
|
||||
print(l$counts)
|
||||
setwd("~/Documents/Masterstudium/1.Semester/DSR_Testate")
|
||||
# Bitte mit echter Lösung in myhistogram.R ersetzen:
|
||||
myhistogram = function(x, n, min, max) {
|
||||
if (missing(min)) min <- base::min(x)
|
||||
if (missing(max)) max <- base::max(x)
|
||||
if (missing(n)) n <- 10
|
||||
if (min >= max) {
|
||||
stop("min muss kleiner als max sein.")
|
||||
}
|
||||
range <- min + max
|
||||
interval_ranges <- (max - min) / n
|
||||
borders <- seq(min, max, length.out = n + 1)
|
||||
counts <- rep(0, n)
|
||||
for(value in x) {
|
||||
for (i in 1:n){
|
||||
if (value >= borders[i] && value < borders[i+1]){
|
||||
counts[i] <- counts[i] + 1
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
print(paste("Intervallgrößen: ", interval_ranges))
|
||||
list(borders=borders, counts = counts)
|
||||
}
|
||||
x = seq(-5, 6, by = 1 / 3)
|
||||
l = myhistogram(x)
|
||||
print(l$borders)
|
||||
print(l$counts)
|
||||
x = seq(-5, 6, by = 1 / 3)
|
||||
l = myhistogram(x, n = 10, min = -5, max = 6)
|
||||
solution = list(
|
||||
borders = c(-5.0, -3.9, -2.8, -1.7, -0.6, 0.5, 1.6, 2.7, 3.8, 4.9, 6.0),
|
||||
counts = c(4, 3, 3, 4, 3, 3, 4, 3, 3, 3)
|
||||
)
|
||||
print(l$borders)
|
||||
print(l$counts)
|
||||
set.seed(1)
|
||||
x = rnorm(0, 1, n = 1000)
|
||||
# h = myhistogram(x, n = 20)
|
||||
# ggplot() + ...
|
||||
# Bitte mit echter Lösung in myhistogram.R ersetzen:
|
||||
myhistogram = function(x, n, min, max) {
|
||||
if (missing(min)) min <- base::min(x)
|
||||
if (missing(max)) max <- base::max(x)
|
||||
if (missing(n)) n <- 10
|
||||
if (min >= max) {
|
||||
stop("min muss kleiner als max sein.")
|
||||
}
|
||||
range <- min + max
|
||||
interval_ranges <- (max - min) / n
|
||||
borders <- seq(min, max, length.out = n + 1)
|
||||
counts <- rep(0, n)
|
||||
for(value in x) {
|
||||
for (i in 1:n){
|
||||
if (value >= borders[i] && value < borders[i+1]){
|
||||
counts[i] <- counts[i] + 1
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
print(paste("Intervallgrößen: ", interval_ranges))
|
||||
list(borders=borders, counts = counts)
|
||||
}
|
||||
x = seq(-5, 6, by = 1 / 3)
|
||||
l = myhistogram(x)
|
||||
print(l$borders)
|
||||
print(l$counts)
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
---
|
||||
title: "Eigenes Histogramm"
|
||||
output: html_document
|
||||
output:
|
||||
html_document: default
|
||||
pdf_document: default
|
||||
---
|
||||
|
||||
# Eigenes Histogramm
|
||||
|
|
@ -20,7 +22,7 @@ Bis auf `x` sollen alle Parameter optional sein. Überlegen Sie sinnvolle Defaul
|
|||
Es ist natürlich **nicht** erlaubt, in der eigenen Funktion andere Funktionen zu nutzen, die ein Histogramm erzeugen.
|
||||
|
||||
Hier ein Beispiel:
|
||||
```{r eval = F}
|
||||
```{r eval = T}
|
||||
# Bitte mit echter Lösung in myhistogram.R ersetzen:
|
||||
myhistogram = function(x, n, min, max) {
|
||||
if (missing(min)) min <- base::min(x)
|
||||
|
|
@ -32,10 +34,15 @@ myhistogram = function(x, n, min, max) {
|
|||
}
|
||||
|
||||
range <- min + max
|
||||
interval_ranges <- (max - min) / n
|
||||
borders <- seq(min, max, length.out = n + 1)
|
||||
counts <- rep(0, n)
|
||||
|
||||
outside <- x[x < min | x >= max]
|
||||
if (length(outside) > 0) {
|
||||
warning(paste("Zahl(en) außerhalb Intervallgrenzen:",
|
||||
paste(outside, collapse = ", ")))
|
||||
}
|
||||
|
||||
for(value in x) {
|
||||
for (i in 1:n){
|
||||
if (value >= borders[i] && value < borders[i+1]){
|
||||
|
|
@ -45,7 +52,6 @@ myhistogram = function(x, n, min, max) {
|
|||
}
|
||||
}
|
||||
|
||||
print(paste("Intervallgrößen: ", interval_ranges))
|
||||
list(borders=borders, counts = counts)
|
||||
}
|
||||
x = seq(-5, 6, by = 1 / 3)
|
||||
|
|
@ -84,22 +90,47 @@ print(l$counts)
|
|||
|
||||
```{r}
|
||||
x = seq(-5, 6, by = 1 / 3)
|
||||
# myhistogram(x, n = 5, min = -10, max = 10)
|
||||
myhistogram(x, n = 5, min = -10, max = 10)
|
||||
solution = list(borders = c(-10, -6, -2, 2, 6, 10),
|
||||
counts = c(0, 9, 12, 12, 1))
|
||||
```
|
||||
|
||||
### Beispiel 3
|
||||
|
||||
Testen Sie nun hier Ihre Funktion mit weiteren Datensätzen.
|
||||
Ganze Zahlen mit Wiederholungen, Default-Werte für `n`, `min` und `max`. Da $b_{n+1}$ selbst zu keinem Intervall gehört, wird das Maximum (10) als außerhalb gemeldet:
|
||||
|
||||
```{r}
|
||||
x = c(1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 6, 7, 8, 9, 10)
|
||||
l = myhistogram(x)
|
||||
print(l$borders)
|
||||
print(l$counts)
|
||||
```
|
||||
|
||||
### Beispiel 4
|
||||
|
||||
Testen Sie nun hier Ihre Funktion mit weiteren Datensätzen.
|
||||
Werte unterhalb von `min` und oberhalb von `max` werden ignoriert und in der Warnung aufgelistet:
|
||||
|
||||
```{r}
|
||||
x = c(-3, -1, 0, 0.5, 1, 2.5, 4, 4.99, 5, 7)
|
||||
l = myhistogram(x, n = 5, min = 0, max = 5)
|
||||
solution = list(borders = c(0, 1, 2, 3, 4, 5),
|
||||
counts = c(2, 1, 1, 0, 2))
|
||||
print(l$borders)
|
||||
print(l$counts)
|
||||
```
|
||||
|
||||
### Beispiel 5
|
||||
|
||||
Testen Sie nun hier Ihre Funktion mit weiteren Datensätzen.
|
||||
Normalverteilte Zufallszahlen, deren Bereich vollständig innerhalb von `min` und `max` liegt (keine Warnung):
|
||||
|
||||
```{r}
|
||||
set.seed(42)
|
||||
x = rnorm(100, mean = 0, sd = 1)
|
||||
l = myhistogram(x, n = 8, min = -4, max = 4)
|
||||
print(l$borders)
|
||||
print(l$counts)
|
||||
print(sum(l$counts)) # muss 100 ergeben
|
||||
```
|
||||
|
||||
|
||||
## Barplot
|
||||
|
|
@ -111,8 +142,19 @@ Tipp: Der Parameter `stat` von `geom_bar` ist wichtig.
|
|||
Vervollständigen Sie den Chunk. Die Kommentare sollen zu Anweisungen umgewandelt werden:
|
||||
|
||||
```{r}
|
||||
library("ggplot2")
|
||||
set.seed(1)
|
||||
x = rnorm(0, 1, n = 1000)
|
||||
# h = myhistogram(x, n = 20)
|
||||
# ggplot() + ...
|
||||
h = myhistogram(x, n = 10)
|
||||
bins = (h$borders[-1]+h$borders[-length(h$borders)])/2 # letzte und erste Grenze entfernen sodass nebeneinanderliegende Grenzen in den Vektoren jeweils auf der gleichen Indexposition liegen. Man verschiebt die Grenzen also
|
||||
|
||||
#print(h$borders[-1])
|
||||
#print(h$borders[length(-h$borders)])
|
||||
|
||||
print(h$borders)
|
||||
count = h$counts #/sum(h$counts) # Damit kann man die relative statt der absoluten Häufigkeit anzeigen
|
||||
ggplot() +
|
||||
geom_bar(aes(x=bins, y=count), stat="identity")+
|
||||
labs(title="Histogramm für normalverteilte Daten", y="Absolute Häufigkeit", x="Intervalle")
|
||||
|
||||
```
|
||||
|
|
|
|||
|
|
@ -386,25 +386,43 @@ Funktion andere Funktionen zu nutzen, die ein Histogramm erzeugen.</p>
|
|||
<p>Hier ein Beispiel:</p>
|
||||
<pre class="r"><code># Bitte mit echter Lösung in myhistogram.R ersetzen:
|
||||
myhistogram = function(x, n, min, max) {
|
||||
if (missing(min)){
|
||||
if (missing(min)) min <- base::min(x)
|
||||
if (missing(max)) max <- base::max(x)
|
||||
if (missing(n)) n <- 10
|
||||
|
||||
if (min >= max) {
|
||||
stop("min muss kleiner als max sein.")
|
||||
}
|
||||
if (missing(max)){
|
||||
|
||||
range <- min + max
|
||||
borders <- seq(min, max, length.out = n + 1)
|
||||
counts <- rep(0, n)
|
||||
|
||||
outside <- x[x < min | x >= max]
|
||||
if (length(outside) > 0) {
|
||||
warning(paste("Zahl(en) außerhalb Intervallgrenzen:",
|
||||
paste(outside, collapse = ", ")))
|
||||
}
|
||||
if (missing(n)){
|
||||
n <- 10
|
||||
|
||||
for(value in x) {
|
||||
for (i in 1:n){
|
||||
if (value >= borders[i] && value < borders[i+1]){
|
||||
counts[i] <- counts[i] + 1
|
||||
break
|
||||
}
|
||||
intervals <- length(x)/n
|
||||
print("Intervalle: ",intervals)
|
||||
print(paste("Intervalel: ", intervals))
|
||||
list(borders = c(), counts = c())
|
||||
}
|
||||
}
|
||||
|
||||
list(borders=borders, counts = counts)
|
||||
}
|
||||
|
||||
x = seq(-5, 6, by = 1 / 3)
|
||||
l = myhistogram(x)
|
||||
print(l$borders)
|
||||
print(l$counts)</code></pre>
|
||||
|
||||
l = myhistogram(x)</code></pre>
|
||||
<pre><code>## Warning in myhistogram(x): Zahl(en) außerhalb Intervallgrenzen: 6</code></pre>
|
||||
<pre class="r"><code>print(l$borders)</code></pre>
|
||||
<pre><code>## [1] -5.0 -3.9 -2.8 -1.7 -0.6 0.5 1.6 2.7 3.8 4.9 6.0</code></pre>
|
||||
<pre class="r"><code>print(l$counts)</code></pre>
|
||||
<pre><code>## [1] 4 3 3 4 3 3 4 3 3 3</code></pre>
|
||||
</div>
|
||||
<div id="beispieldaten" class="section level2">
|
||||
<h2>Beispieldaten</h2>
|
||||
|
|
@ -415,30 +433,71 @@ print(l$counts)</code></pre>
|
|||
<pre><code>## Warning in myhistogram(x, n = 10, min = -5, max = 6): Zahl(en) außerhalb
|
||||
## Intervallgrenzen: 6</code></pre>
|
||||
<pre class="r"><code>x = seq(-5, 6, by = 1 / 3)
|
||||
# myhistogram(x, n = 10, min = -5, max = 6)
|
||||
solution = list(
|
||||
l = myhistogram(x, n = 10, min = -5, max = 6)</code></pre>
|
||||
<pre><code>## Warning in myhistogram(x, n = 10, min = -5, max = 6): Zahl(en) außerhalb
|
||||
## Intervallgrenzen: 6</code></pre>
|
||||
<pre class="r"><code>solution = list(
|
||||
borders = c(-5.0, -3.9, -2.8, -1.7, -0.6, 0.5, 1.6, 2.7, 3.8, 4.9, 6.0),
|
||||
counts = c(4, 3, 3, 4, 3, 3, 4, 3, 3, 3)
|
||||
)</code></pre>
|
||||
)
|
||||
print(l$borders)</code></pre>
|
||||
<pre><code>## [1] -5.0 -3.9 -2.8 -1.7 -0.6 0.5 1.6 2.7 3.8 4.9 6.0</code></pre>
|
||||
<pre class="r"><code>print(l$counts)</code></pre>
|
||||
<pre><code>## [1] 4 3 3 4 3 3 4 3 3 3</code></pre>
|
||||
</div>
|
||||
<div id="beispiel-2" class="section level3">
|
||||
<h3>Beispiel 2</h3>
|
||||
<pre class="r"><code>x = seq(-5, 6, by = 1 / 3)
|
||||
# myhistogram(x, n = 5, min = -10, max = 10)
|
||||
solution = list(borders = c(-10, -6, -2, 2, 6, 10),
|
||||
myhistogram(x, n = 5, min = -10, max = 10)</code></pre>
|
||||
<pre><code>## $borders
|
||||
## [1] -10 -6 -2 2 6 10
|
||||
##
|
||||
## $counts
|
||||
## [1] 0 9 12 12 1</code></pre>
|
||||
<pre class="r"><code>solution = list(borders = c(-10, -6, -2, 2, 6, 10),
|
||||
counts = c(0, 9, 12, 12, 1))</code></pre>
|
||||
</div>
|
||||
<div id="beispiel-3" class="section level3">
|
||||
<h3>Beispiel 3</h3>
|
||||
<p>Testen Sie nun hier Ihre Funktion mit weiteren Datensätzen.</p>
|
||||
<p>Ganze Zahlen mit Wiederholungen, Default-Werte für <code>n</code>,
|
||||
<code>min</code> und <code>max</code>. Da <span class="math inline">\(b_{n+1}\)</span> selbst zu keinem Intervall
|
||||
gehört, wird das Maximum (10) als außerhalb gemeldet:</p>
|
||||
<pre class="r"><code>x = c(1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 6, 7, 8, 9, 10)
|
||||
l = myhistogram(x)</code></pre>
|
||||
<pre><code>## Warning in myhistogram(x): Zahl(en) außerhalb Intervallgrenzen: 10</code></pre>
|
||||
<pre class="r"><code>print(l$borders)</code></pre>
|
||||
<pre><code>## [1] 1.0 1.9 2.8 3.7 4.6 5.5 6.4 7.3 8.2 9.1 10.0</code></pre>
|
||||
<pre class="r"><code>print(l$counts)</code></pre>
|
||||
<pre><code>## [1] 1 2 3 4 1 1 1 1 1 0</code></pre>
|
||||
</div>
|
||||
<div id="beispiel-4" class="section level3">
|
||||
<h3>Beispiel 4</h3>
|
||||
<p>Testen Sie nun hier Ihre Funktion mit weiteren Datensätzen.</p>
|
||||
<p>Werte unterhalb von <code>min</code> und oberhalb von
|
||||
<code>max</code> werden ignoriert und in der Warnung aufgelistet:</p>
|
||||
<pre class="r"><code>x = c(-3, -1, 0, 0.5, 1, 2.5, 4, 4.99, 5, 7)
|
||||
l = myhistogram(x, n = 5, min = 0, max = 5)</code></pre>
|
||||
<pre><code>## Warning in myhistogram(x, n = 5, min = 0, max = 5): Zahl(en) außerhalb
|
||||
## Intervallgrenzen: -3, -1, 5, 7</code></pre>
|
||||
<pre class="r"><code>solution = list(borders = c(0, 1, 2, 3, 4, 5),
|
||||
counts = c(2, 1, 1, 0, 2))
|
||||
print(l$borders)</code></pre>
|
||||
<pre><code>## [1] 0 1 2 3 4 5</code></pre>
|
||||
<pre class="r"><code>print(l$counts)</code></pre>
|
||||
<pre><code>## [1] 2 1 1 0 2</code></pre>
|
||||
</div>
|
||||
<div id="beispiel-5" class="section level3">
|
||||
<h3>Beispiel 5</h3>
|
||||
<p>Testen Sie nun hier Ihre Funktion mit weiteren Datensätzen.</p>
|
||||
<p>Normalverteilte Zufallszahlen, deren Bereich vollständig innerhalb
|
||||
von <code>min</code> und <code>max</code> liegt (keine Warnung):</p>
|
||||
<pre class="r"><code>set.seed(42)
|
||||
x = rnorm(100, mean = 0, sd = 1)
|
||||
l = myhistogram(x, n = 8, min = -4, max = 4)
|
||||
print(l$borders)</code></pre>
|
||||
<pre><code>## [1] -4 -3 -2 -1 0 1 2 3 4</code></pre>
|
||||
<pre class="r"><code>print(l$counts)</code></pre>
|
||||
<pre><code>## [1] 0 4 11 31 37 15 2 0</code></pre>
|
||||
<pre class="r"><code>print(sum(l$counts)) # muss 100 ergeben</code></pre>
|
||||
<pre><code>## [1] 100</code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<div id="barplot" class="section level2">
|
||||
|
|
|
|||
Loading…
Reference in New Issue