forked from joanby/r-basic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01-table.Rmd
60 lines (47 loc) · 1.1 KB
/
01-table.Rmd
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
---
title: "Tablas de Contingencia"
author: "Curso de Estadística Descriptiva"
date: "24/12/2018"
output: html_document
---
# Tablas de contingencia
```{r}
datos = factor(c("H", "M", "M", "M", "H", "H", "M", "M"))
table(datos)
table(datos)["M"]
sum(table(datos))
```
# Frecuencias relativas
$$f_i = \frac{n_i}{n}$$
```{r}
prop.table(table(datos))
100*prop.table(table(datos))
table(datos)/length(datos)
names(which(table(datos)==3))
moda <- function(d){
names(which(table(d)==max(table(d))))
}
m_t = moda(datos)
```
La moda del data frame es: `r m_t`.
# Paquete `gmodels`
```{r}
library(gmodels)
sex = factor(c("H", "M", "M", "M", "H", "H", "M", "M"))
ans = factor(c("S", "N", "S", "S", "S", "N", "N", "S"))
CrossTable(sex, ans, prop.chisq = FALSE)
```
# Sumas por filas y columnas
```{r}
tt <- table(sex, ans)
tt# Frec. absolutas
prop.table(tt)#Frec. Rel. Global
prop.table(tt, margin = 1)#Frec. Rel. Por sexo
prop.table(tt, margin = 2)#Frec. Rel. Por respuesta
colSums(tt)
rowSums(tt)
colSums(prop.table(tt))
rowSums(prop.table(tt))
apply(tt, FUN = sum, MARGIN = 1)
apply(tt, FUN = sqrt, MARGIN = c(1,2))
```