---
# System prepended metadata

title: Clase del 30 agosto 2022
tags: [Curso IPE 2022]

---

# Clase del 30 agosto 2022

###### tags: `Curso IPE 2022`

## Objetivos

- Muestreo aleatorio y cálculo de frecuencia.
- Graficar un histograma.

## Funcionalidades

- Saber usar la funcion `sample` con y sin reposición.
- Comando `table`.
- Comando `barplot`.


## Ejercicio 1

Consideramos el ejercicio 11 del practico 1:

> De cuantas maneras se pueden ordenar 5 libros en una fila?

Llamemosle "A", "B", "C", "D", "E" a los libros.

```R
x <- c("A", "B", "C", "D", "E")
x <- LETTERS[1:5]

> sample(x, 5, replace = FALSE)
[1] "C" "E" "A" "D" "B"
> sample(x, 5, replace = FALSE)
[1] "C" "E" "B" "D" "A"
> sample(x, 5, replace = FALSE)
[1] "D" "C" "A" "B" "E"
> sample(x, 5, replace = FALSE)
[1] "A" "E" "C" "B" "D"
> sample(x, 5, replace = FALSE)
[1] "D" "E" "B" "C" "A"
```


## Permutaciones

- https://www.r-bloggers.com/2020/09/permutations-in-r/
- https://stackoverflow.com/questions/22569176/how-to-generate-permutations-or-combinations-of-object-in-r/47983855#47983855

### Para instalar

```R
> install.packages("combinat")
```

```R
> install.packages("combinat")
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.2/combinat_0.0-8.tgz'
Content type 'application/x-gzip' length 40732 bytes (39 KB)
==================================================
downloaded 39 KB


The downloaded binary packages are in
	/var/folders/0p/w0s_xyq10178j9qhlz_0h90m0000gp/T//Rtmp5XfM1H/downloaded_packages
```

### Para utilizar

```R
> library(combinat)
```

```R
> permn(1:3)
[[1]]
[1] 1 2 3

[[2]]
[1] 1 3 2

[[3]]
[1] 3 1 2

[[4]]
[1] 3 2 1

[[5]]
[1] 2 3 1

[[6]]
[1] 2 1 3
```

```R
> ?permn
```

```R
> permn(3, sum)
[[1]]
[1] 6

[[2]]
[1] 6

[[3]]
[1] 6

[[4]]
[1] 6

[[5]]
[1] 6

[[6]]
[1] 6
```

```R
> x <- LETTER[1:5]

> length(permn(x))
[1] 120
```

```R
> factorial(5)
[1] 120
```

## Ejercicio 2

```R
> x <- LETTERS[1:5]
> y <- sample(x, 10, replace=TRUE)
> y
 [1] "B" "E" "A" "B" "B" "D" "D" "D" "B"
[10] "A"
```

```R
> t <- table(y)
> t
y
A B D E 
2 4 3 1

barplot(t)
```

```R
> sum(t)
[1] 10
```

```R
> tf <- t / sum(t)
> tf
y
  A   B   D   E 
0.2 0.4 0.3 0.1
```

```R
barplot(tf)
```

Para ver los porcentajes:

```R
> tp <- t / sum(t) * 100
> tp
y
 A  B  D  E 
20 40 30 10
```


## Ejercicio

Repetir el ejercicio anterior pero con:

>  ¿Cuantos mensajes telegraficos se pueden mandar con 5 puntos y 3 rayas ?