-
Notifications
You must be signed in to change notification settings - Fork 4
/
r-intro.R
161 lines (124 loc) · 5.1 KB
/
r-intro.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
help.start()
install.packages("Rcmdr")
library("Rcmdr")
is.list(mtcars)
mod <- lm(mpg ~ wt, data = mtcars)
is.list(mod)
## ------------------------------------------------------------------------
"Hola mundo!"
# Esto es un comentario
y <- 2 + 2 # Asignación
y^y / 10*pi + 1 # Expresión
matrix(1, nrow = y/2, ncol = y)
## ------------------------------------------------------------------------
str(matrix(data = 1, y/2, y))
## ----vectores------------------------------------------------------------
logico <- c(TRUE, FALSE, T)
entero <- c(1L, 6L, 10L)
doble <- c(a = 1, b = 2.5, c = 4.5)
caracter <- c("Hola", "mundo", "!")
## ------------------------------------------------------------------------
recursivo <- list(id = c(1, 2, 3), var = c("a", "b", "c"),
lista = list(4:9, TRUE))
str(recursivo)
## ------------------------------------------------------------------------
## Las tres propiedades principales
typeof(logico)
length(entero)
attributes(doble)
## ------------------------------------------------------------------------
# Las funciones is.* y as.* logical, integer, double, character, list
is.character(caracter)
as.integer(logico)
# Coerción por defecto:
mean(logico)
## ------------------------------------------------------------------------
str(attributes(doble))
## ------------------------------------------------------------------------
class(doble) # Se pueden modificar: class(doble) <- "myclass"
names(doble)
dim(doble)
## ------------------------------------------------------------------------
NA # logico
NA_integer_; NA_real_; NA_character_;
## ------------------------------------------------------------------------
categoria <- factor(x = c("A", "B", "A", "A", "B"))
typeof(categoria)
attributes(categoria)
table(categoria)
## ------------------------------------------------------------------------
matriz <- matrix(data = runif(4), ncol = 2, nrow = 2)
arreglo <- array(data = rnorm(24), c(2, 3, 4))
dim(caracter) <- c(1, 3)
caracter
str(arreglo)
ncol(matriz); dim(arreglo)
## ------------------------------------------------------------------------
# Nada - util para más dimensiones
doble[]
# Entero positivo
doble[c(1, 2)]
# Entero negativo
doble[-c(1, 2)]
## ------------------------------------------------------------------------
# Logico
doble[doble > 1]
# Caracter
doble[c("a", "c", "c")]
## ------------------------------------------------------------------------
matriz
matriz[, c(TRUE, FALSE)]
matriz[1, , drop = FALSE]
## ------------------------------------------------------------------------
recursivo[[1]]
recursivo$lista[[1]][3:5]
## ------------------------------------------------------------------------
(df <- data.frame(x = c("a", "b", "c"), y = 1:3))
df[2, 2] # Se pueden seleccionar como en las matrices
df$y[2] # Se pueden seleccionar como en las listas
## ------------------------------------------------------------------------
df1 <- data.frame(x = c("a", "b"), y = 1:2)
df2 <- data.frame(x = c("c", "d"), y = c(6, 7))
(df <- rbind(df1, df2))
## ------------------------------------------------------------------------
# Importar de un csv (Enfoque malo)
dat <- read.csv("./data/saber2014II.csv", sep = ";", dec=",")
# head(dat)
# Importar de un csv (Enfoque correcto)
clases <- c(rep("character", 8), rep("numeric", 8), rep("NULL", 7), "character")
datos <- read.csv("./data/saber2014II.csv", sep = ";", dec=",", colClasses = clases)
## ------------------------------------------------------------------------
library(readr)
datos <- read_csv2("./data/saber2014II.csv")
# Nuevo Paquete de Whickham para importar de excel
library(readxl)
saber <- read_excel("./data/saber2014II.xlsx")
# Importar de SPSS con etiquetas
library(haven)
saber <- read_spss("./data/saber2014II.sav")
## ------------------------------------------------------------------------
library(dplyr)
resumen_a <- datos %>%
mutate(PROMGLOBAL = (PROMMATEMATICA + PROMINGLES + PROMLECTURACRITICA +
PROMSOCIALESYCIUDADANAS + PROMCIENCIASNATURALES +
PROMRAZONAMIENTOCUANTITA + PROMCOMPETENCIASCIUDADAN)/7) %>%
group_by(NOMBREMUNICIPIO) %>%
summarize(DEPARTAMENTO = max(DEPARTAMENTO), TOTAL_EVALUADOS = sum(EVALUADOS)) %>%
arrange(desc(TOTAL_EVALUADOS)) %>%
mutate(ranking = row_number()) %>%
select(ranking, DEPARTAMENTO, NOMBREMUNICIPIO, TOTAL_EVALUADOS)
## ------------------------------------------------------------------------
resumen_a
## ------------------------------------------------------------------------
resumen_b <- datos %>%
mutate(PROMGLOBAL = (PROMMATEMATICA + PROMINGLES + PROMLECTURACRITICA +
PROMSOCIALESYCIUDADANAS + PROMCIENCIASNATURALES +
PROMRAZONAMIENTOCUANTITA + PROMCOMPETENCIASCIUDADAN)/7) %>%
group_by(NOMBREMUNICIPIO) %>%
summarize(DEPARTAMENTO = max(DEPARTAMENTO),
PROMGLOBALXMPIO = sum(PROMGLOBAL * EVALUADOS) / sum(EVALUADOS)) %>%
arrange(desc(PROMGLOBALXMPIO)) %>%
mutate(ranking = row_number()) %>%
select(ranking, DEPARTAMENTO, NOMBREMUNICIPIO, PROMGLOBALXMPIO)
## ------------------------------------------------------------------------
resumen_b