cleanup: add merge solution PDF, move exercises to separate folder each, add gitignore and delete readme
parent
7084ce3a51
commit
e38f953019
423
.Rhistory
423
.Rhistory
|
|
@ -1,423 +0,0 @@
|
||||||
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)
|
|
||||||
|
|
@ -0,0 +1,59 @@
|
||||||
|
# Created by https://www.toptal.com/developers/gitignore/api/r
|
||||||
|
# Edit at https://www.toptal.com/developers/gitignore?templates=r
|
||||||
|
|
||||||
|
### R ###
|
||||||
|
# History files
|
||||||
|
.Rhistory
|
||||||
|
.Rapp.history
|
||||||
|
|
||||||
|
# Session Data files
|
||||||
|
.RData
|
||||||
|
.RDataTmp
|
||||||
|
|
||||||
|
# User-specific files
|
||||||
|
.Ruserdata
|
||||||
|
|
||||||
|
# Example code in package build process
|
||||||
|
*-Ex.R
|
||||||
|
|
||||||
|
# Output files from R CMD build
|
||||||
|
/*.tar.gz
|
||||||
|
|
||||||
|
# Output files from R CMD check
|
||||||
|
/*.Rcheck/
|
||||||
|
|
||||||
|
# RStudio files
|
||||||
|
.Rproj.user/
|
||||||
|
|
||||||
|
# produced vignettes
|
||||||
|
vignettes/*.html
|
||||||
|
vignettes/*.pdf
|
||||||
|
|
||||||
|
# OAuth2 token, see https://github.com/hadley/httr/releases/tag/v0.3
|
||||||
|
.httr-oauth
|
||||||
|
|
||||||
|
# knitr and R markdown default cache directories
|
||||||
|
*_cache/
|
||||||
|
/cache/
|
||||||
|
|
||||||
|
# Temporary files created by R markdown
|
||||||
|
*.utf8.md
|
||||||
|
*.knit.md
|
||||||
|
|
||||||
|
# R Environment Variables
|
||||||
|
.Renviron
|
||||||
|
|
||||||
|
# pkgdown site
|
||||||
|
docs/
|
||||||
|
|
||||||
|
# translation temp files
|
||||||
|
po/*~
|
||||||
|
|
||||||
|
# RStudio Connect folder
|
||||||
|
rsconnect/
|
||||||
|
|
||||||
|
### R.Bookdown Stack ###
|
||||||
|
# R package: bookdown caching files
|
||||||
|
/*_files/
|
||||||
|
|
||||||
|
# End of https://www.toptal.com/developers/gitignore/api/r
|
||||||
Binary file not shown.
|
|
@ -1,8 +1,8 @@
|
||||||
---
|
---
|
||||||
title: "Eigenes Histogramm"
|
title: "Eigenes Histogramm"
|
||||||
output:
|
output:
|
||||||
html_document: default
|
|
||||||
pdf_document: default
|
pdf_document: default
|
||||||
|
html_document: default
|
||||||
---
|
---
|
||||||
|
|
||||||
# Eigenes Histogramm
|
# Eigenes Histogramm
|
||||||
|
|
@ -26,18 +26,23 @@ Hier ein Beispiel:
|
||||||
# Bitte mit echter Lösung in myhistogram.R ersetzen:
|
# Bitte mit echter Lösung in myhistogram.R ersetzen:
|
||||||
myhistogram = function(x, n, min, max) {
|
myhistogram = function(x, n, min, max) {
|
||||||
if (missing(min)) min <- base::min(x)
|
if (missing(min)) min <- base::min(x)
|
||||||
if (missing(max)) max <- base::max(x)
|
|
||||||
|
# sonst fällt max(x) wegen b_i <= z < b_{i+1} raus
|
||||||
|
if (missing(max)) max <- base::max(x) + 10^-12
|
||||||
|
|
||||||
if (missing(n)) n <- 10
|
if (missing(n)) n <- 10
|
||||||
|
|
||||||
if (min >= max) {
|
if (min >= max) {
|
||||||
stop("min muss kleiner als max sein.")
|
stop("min muss kleiner als max sein.")
|
||||||
}
|
}
|
||||||
|
|
||||||
range <- min + max
|
# Äquidistante Aufteilung der Eingabe in n Intervalle mit n + 1 Grenzen
|
||||||
borders <- seq(min, max, length.out = n + 1)
|
borders <- seq(min, max, length.out = n + 1)
|
||||||
counts <- rep(0, n)
|
counts <- rep(0, n)
|
||||||
|
|
||||||
|
# Werte außerhalb des gewählten Intervalls ermitteln
|
||||||
outside <- x[x < min | x >= max]
|
outside <- x[x < min | x >= max]
|
||||||
|
|
||||||
if (length(outside) > 0) {
|
if (length(outside) > 0) {
|
||||||
warning(paste("Zahl(en) außerhalb Intervallgrenzen:",
|
warning(paste("Zahl(en) außerhalb Intervallgrenzen:",
|
||||||
paste(outside, collapse = ", ")))
|
paste(outside, collapse = ", ")))
|
||||||
|
|
@ -45,13 +50,16 @@ myhistogram = function(x, n, min, max) {
|
||||||
|
|
||||||
for(value in x) {
|
for(value in x) {
|
||||||
for (i in 1:n){
|
for (i in 1:n){
|
||||||
if (value >= borders[i] && value < borders[i+1]){
|
# Linke Grenze inklusive, rechte exklusive
|
||||||
|
# Deswegen wurde oben auf max 10^-12 addiert
|
||||||
|
if (value >= borders[i] && value < borders[i+1])
|
||||||
|
{
|
||||||
counts[i] <- counts[i] + 1
|
counts[i] <- counts[i] + 1
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
# Intervallgrenzen und absolute Häufigkeit zurückgeben
|
||||||
list(borders=borders, counts = counts)
|
list(borders=borders, counts = counts)
|
||||||
}
|
}
|
||||||
x = seq(-5, 6, by = 1 / 3)
|
x = seq(-5, 6, by = 1 / 3)
|
||||||
|
|
@ -97,7 +105,7 @@ solution = list(borders = c(-10, -6, -2, 2, 6, 10),
|
||||||
|
|
||||||
### Beispiel 3
|
### Beispiel 3
|
||||||
|
|
||||||
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:
|
Ganze Zahlen mit Wiederholungen, Default-Werte für `n`, `min` und `max`
|
||||||
|
|
||||||
```{r}
|
```{r}
|
||||||
x = c(1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 6, 7, 8, 9, 10)
|
x = c(1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 6, 7, 8, 9, 10)
|
||||||
|
|
@ -112,6 +120,8 @@ Werte unterhalb von `min` und oberhalb von `max` werden ignoriert und in der War
|
||||||
|
|
||||||
```{r}
|
```{r}
|
||||||
x = c(-3, -1, 0, 0.5, 1, 2.5, 4, 4.99, 5, 7)
|
x = c(-3, -1, 0, 0.5, 1, 2.5, 4, 4.99, 5, 7)
|
||||||
|
|
||||||
|
# da max explizit gesetzt wird, fallen 5 und 7 aus den gütligen Grenzen raus
|
||||||
l = myhistogram(x, n = 5, min = 0, max = 5)
|
l = myhistogram(x, n = 5, min = 0, max = 5)
|
||||||
solution = list(borders = c(0, 1, 2, 3, 4, 5),
|
solution = list(borders = c(0, 1, 2, 3, 4, 5),
|
||||||
counts = c(2, 1, 1, 0, 2))
|
counts = c(2, 1, 1, 0, 2))
|
||||||
|
|
@ -145,16 +155,21 @@ Vervollständigen Sie den Chunk. Die Kommentare sollen zu Anweisungen umgewandel
|
||||||
library("ggplot2")
|
library("ggplot2")
|
||||||
set.seed(1)
|
set.seed(1)
|
||||||
x = rnorm(0, 1, n = 1000)
|
x = rnorm(0, 1, n = 1000)
|
||||||
h = myhistogram(x, n = 10)
|
h = myhistogram(x)
|
||||||
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
|
# letzte und erste Grenze entfernen sodass nebeneinanderliegende Grenzen in den Vektoren
|
||||||
|
# jeweils auf der gleichen Indexposition liegen. Man verschiebt die Grenzen also
|
||||||
|
bins = (h$borders[-1]+h$borders[-length(h$borders)])/2
|
||||||
|
|
||||||
#print(h$borders[-1])
|
#print(h$borders[-1])
|
||||||
#print(h$borders[length(-h$borders)])
|
#print(h$borders[length(-h$borders)])
|
||||||
|
|
||||||
print(h$borders)
|
print(h$borders)
|
||||||
count = h$counts #/sum(h$counts) # Damit kann man die relative statt der absoluten Häufigkeit anzeigen
|
# Damit kann man die relative statt der absoluten Häufigkeit anzeigen
|
||||||
|
count = h$counts #/sum(h$counts)
|
||||||
|
|
||||||
ggplot() +
|
ggplot() +
|
||||||
geom_bar(aes(x=bins, y=count), stat="identity")+
|
geom_bar(aes(x=bins, y=count), stat="identity")+
|
||||||
labs(title="Histogramm für normalverteilte Daten", y="Absolute Häufigkeit", x="Intervalle")
|
labs(title="Histogramm für normalverteilte Daten", y="Absolute Häufigkeit", x="Intervalle")
|
||||||
|
|
||||||
|
print(h$counts)
|
||||||
```
|
```
|
||||||
File diff suppressed because one or more lines are too long
Binary file not shown.
Loading…
Reference in New Issue