Skip to content

Commit

Permalink
changes practico 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin-carrasco committed Apr 24, 2024
1 parent d6cda5b commit bbea644
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 81 deletions.
22 changes: 6 additions & 16 deletions assignment/03-practico.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,12 @@ La estructura general para importar datos es la siguiente:
`read_*("ruta_hacia_archivo/nombre_archivo.*")`

```{r include=FALSE}
elsoc_2022 <- haven::read_sav("input/data/ELSOC_W06_v1.0_SPSS.sav")
```


```{r eval=FALSE, include=TRUE}
elsoc_2022 <- read_sav("input/data/ELSOC_W06_v1.0_SPSS.sav")
```

::: callout-note
Expand Down Expand Up @@ -271,33 +268,26 @@ La estructura de `mutate()` es generalmente esta:
Recodifiquemos las variables sexo e ingresos:

```{r collapse=FALSE, warning=FALSE}
proc_elsoc$sexo <- car::recode(proc_elsoc$sexo, recodes = c("'Hombre' = 'Masculino'; 'Mujer' = 'Femenino'"))
proc_elsoc <- dplyr::mutate(proc_elsoc,
sexo = car::recode(sexo,
recodes = c("'Hombre' = 'Masculino'; 'Mujer' = 'Femenino'")),
ingreso = car::recode(ingreso,
recodes = c("-888 = NA; -999 = NA")))
proc_elsoc$ingreso <- car::recode(proc_elsoc$ingreso, recodes = c("-888 = NA; -999 = NA"))
proc_elsoc
```

Ahora recodifiquemos las demás variables. Además de recodificar valores propiamente tal, con `recode()` podemos indicarle, en la misma función, que convierta la variable a `factor` y/o que le asigne niveles (ej. para variables ordinales).

```{r collapse=FALSE, warning=FALSE}
proc_elsoc <- dplyr::mutate(proc_elsoc,
autor_democ = car::recode(autor_democ,
proc_elsoc$autor_democ <- car::recode(proc_elsoc$autor_democ,
recodes = c("1 = 'La democracia es preferible a cualquier otra forma de gobierno';
2 = 'En algunas circunstancias, un gobierno autoritario puede ser preferible a uno democratico';
3 = 'A la gente como uno, nos da lo mismo un regimen democratico que uno autoritario';
4 = 'Ninguna';
-888 = NA;
-999 = NA"),
as.factor = TRUE)) # convertir a factor
as.factor = TRUE) # convertir a factor
proc_elsoc <- dplyr::mutate(proc_elsoc,
jv_delincuentes = car::recode(jv_delincuentes,
proc_elsoc$jv_delincuentes <- car::recode(proc_elsoc$jv_delincuentes,
recodes = c("1 = 'Nunca';
2 = 'Pocas veces';
3 = 'Algunas veces';
Expand All @@ -310,7 +300,7 @@ proc_elsoc <- dplyr::mutate(proc_elsoc,
"Pocas veces",
"Algunas veces",
"Muchas veces",
"Siempre")))# ordenamos niveles
"Siempre"))# ordenamos niveles
proc_elsoc
```
Expand Down
65 changes: 30 additions & 35 deletions docs/assignment/03-practico.html
Original file line number Diff line number Diff line change
Expand Up @@ -612,14 +612,11 @@ <h3 class="anchored" data-anchor-id="recodificar">4.3 Recodificar</h3>
<p><code>dplyr::mutate(datos, nueva_variable = funcion())</code></p>
<p>Recodifiquemos las variables sexo e ingresos:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb23"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a>proc_elsoc <span class="ot">&lt;-</span> dplyr<span class="sc">::</span><span class="fu">mutate</span>(proc_elsoc,</span>
<span id="cb23-2"><a href="#cb23-2" aria-hidden="true" tabindex="-1"></a> <span class="at">sexo =</span> car<span class="sc">::</span><span class="fu">recode</span>(sexo,</span>
<span id="cb23-3"><a href="#cb23-3" aria-hidden="true" tabindex="-1"></a> <span class="at">recodes =</span> <span class="fu">c</span>(<span class="st">"'Hombre' = 'Masculino'; 'Mujer' = 'Femenino'"</span>)),</span>
<span id="cb23-4"><a href="#cb23-4" aria-hidden="true" tabindex="-1"></a> <span class="at">ingreso =</span> car<span class="sc">::</span><span class="fu">recode</span>(ingreso, </span>
<span id="cb23-5"><a href="#cb23-5" aria-hidden="true" tabindex="-1"></a> <span class="at">recodes =</span> <span class="fu">c</span>(<span class="st">"-888 = NA; -999 = NA"</span>)))</span>
<span id="cb23-6"><a href="#cb23-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb23-7"><a href="#cb23-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb23-8"><a href="#cb23-8" aria-hidden="true" tabindex="-1"></a>proc_elsoc</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="sourceCode cell-code" id="cb23"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a>proc_elsoc<span class="sc">$</span>sexo <span class="ot">&lt;-</span> car<span class="sc">::</span><span class="fu">recode</span>(proc_elsoc<span class="sc">$</span>sexo, <span class="at">recodes =</span> <span class="fu">c</span>(<span class="st">"'Hombre' = 'Masculino'; 'Mujer' = 'Femenino'"</span>))</span>
<span id="cb23-2"><a href="#cb23-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb23-3"><a href="#cb23-3" aria-hidden="true" tabindex="-1"></a>proc_elsoc<span class="sc">$</span>ingreso <span class="ot">&lt;-</span> car<span class="sc">::</span><span class="fu">recode</span>(proc_elsoc<span class="sc">$</span>ingreso, <span class="at">recodes =</span> <span class="fu">c</span>(<span class="st">"-888 = NA; -999 = NA"</span>))</span>
<span id="cb23-4"><a href="#cb23-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb23-5"><a href="#cb23-5" aria-hidden="true" tabindex="-1"></a>proc_elsoc</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 980 × 5
edad sexo ingreso autor_democ jv_delincuentes
Expand All @@ -639,33 +636,31 @@ <h3 class="anchored" data-anchor-id="recodificar">4.3 Recodificar</h3>
</div>
<p>Ahora recodifiquemos las demás variables. Además de recodificar valores propiamente tal, con <code>recode()</code> podemos indicarle, en la misma función, que convierta la variable a <code>factor</code> y/o que le asigne niveles (ej. para variables ordinales).</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb25"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a>proc_elsoc <span class="ot">&lt;-</span> dplyr<span class="sc">::</span><span class="fu">mutate</span>(proc_elsoc,</span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a> <span class="at">autor_democ =</span> car<span class="sc">::</span><span class="fu">recode</span>(autor_democ,</span>
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a> <span class="at">recodes =</span> <span class="fu">c</span>(<span class="st">"1 = 'La democracia es preferible a cualquier otra forma de gobierno'; </span></span>
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a><span class="st"> 2 = 'En algunas circunstancias, un gobierno autoritario puede ser preferible a uno democratico'; </span></span>
<span id="cb25-5"><a href="#cb25-5" aria-hidden="true" tabindex="-1"></a><span class="st"> 3 = 'A la gente como uno, nos da lo mismo un regimen democratico que uno autoritario'; </span></span>
<span id="cb25-6"><a href="#cb25-6" aria-hidden="true" tabindex="-1"></a><span class="st"> 4 = 'Ninguna'; </span></span>
<span id="cb25-7"><a href="#cb25-7" aria-hidden="true" tabindex="-1"></a><span class="st"> -888 = NA; </span></span>
<span id="cb25-8"><a href="#cb25-8" aria-hidden="true" tabindex="-1"></a><span class="st"> -999 = NA"</span>),</span>
<span id="cb25-9"><a href="#cb25-9" aria-hidden="true" tabindex="-1"></a> <span class="at">as.factor =</span> <span class="cn">TRUE</span>)) <span class="co"># convertir a factor</span></span>
<span id="cb25-10"><a href="#cb25-10" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb25-11"><a href="#cb25-11" aria-hidden="true" tabindex="-1"></a>proc_elsoc <span class="ot">&lt;-</span> dplyr<span class="sc">::</span><span class="fu">mutate</span>(proc_elsoc,</span>
<span id="cb25-12"><a href="#cb25-12" aria-hidden="true" tabindex="-1"></a> <span class="at">jv_delincuentes =</span> car<span class="sc">::</span><span class="fu">recode</span>(jv_delincuentes,</span>
<span id="cb25-13"><a href="#cb25-13" aria-hidden="true" tabindex="-1"></a> <span class="at">recodes =</span> <span class="fu">c</span>(<span class="st">"1 = 'Nunca';</span></span>
<span id="cb25-14"><a href="#cb25-14" aria-hidden="true" tabindex="-1"></a><span class="st"> 2 = 'Pocas veces';</span></span>
<span id="cb25-15"><a href="#cb25-15" aria-hidden="true" tabindex="-1"></a><span class="st"> 3 = 'Algunas veces';</span></span>
<span id="cb25-16"><a href="#cb25-16" aria-hidden="true" tabindex="-1"></a><span class="st"> 4 = 'Muchas veces';</span></span>
<span id="cb25-17"><a href="#cb25-17" aria-hidden="true" tabindex="-1"></a><span class="st"> 5 = 'Siempre';</span></span>
<span id="cb25-18"><a href="#cb25-18" aria-hidden="true" tabindex="-1"></a><span class="st"> -888 = NA; </span></span>
<span id="cb25-19"><a href="#cb25-19" aria-hidden="true" tabindex="-1"></a><span class="st"> -999 = NA"</span>),</span>
<span id="cb25-20"><a href="#cb25-20" aria-hidden="true" tabindex="-1"></a> <span class="at">as.factor =</span> <span class="cn">TRUE</span>, <span class="co"># convertir a factor</span></span>
<span id="cb25-21"><a href="#cb25-21" aria-hidden="true" tabindex="-1"></a> <span class="at">levels =</span> <span class="fu">c</span>(<span class="st">"Nunca"</span>,</span>
<span id="cb25-22"><a href="#cb25-22" aria-hidden="true" tabindex="-1"></a> <span class="st">"Pocas veces"</span>,</span>
<span id="cb25-23"><a href="#cb25-23" aria-hidden="true" tabindex="-1"></a> <span class="st">"Algunas veces"</span>,</span>
<span id="cb25-24"><a href="#cb25-24" aria-hidden="true" tabindex="-1"></a> <span class="st">"Muchas veces"</span>,</span>
<span id="cb25-25"><a href="#cb25-25" aria-hidden="true" tabindex="-1"></a> <span class="st">"Siempre"</span>)))<span class="co"># ordenamos niveles</span></span>
<span id="cb25-26"><a href="#cb25-26" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb25-27"><a href="#cb25-27" aria-hidden="true" tabindex="-1"></a>proc_elsoc</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="sourceCode cell-code" id="cb25"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a>proc_elsoc<span class="sc">$</span>autor_democ <span class="ot">&lt;-</span> car<span class="sc">::</span><span class="fu">recode</span>(proc_elsoc<span class="sc">$</span>autor_democ,</span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a> <span class="at">recodes =</span> <span class="fu">c</span>(<span class="st">"1 = 'La democracia es preferible a cualquier otra forma de gobierno'; </span></span>
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a><span class="st"> 2 = 'En algunas circunstancias, un gobierno autoritario puede ser preferible a uno democratico'; </span></span>
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a><span class="st"> 3 = 'A la gente como uno, nos da lo mismo un regimen democratico que uno autoritario'; </span></span>
<span id="cb25-5"><a href="#cb25-5" aria-hidden="true" tabindex="-1"></a><span class="st"> 4 = 'Ninguna'; </span></span>
<span id="cb25-6"><a href="#cb25-6" aria-hidden="true" tabindex="-1"></a><span class="st"> -888 = NA; </span></span>
<span id="cb25-7"><a href="#cb25-7" aria-hidden="true" tabindex="-1"></a><span class="st"> -999 = NA"</span>),</span>
<span id="cb25-8"><a href="#cb25-8" aria-hidden="true" tabindex="-1"></a> <span class="at">as.factor =</span> <span class="cn">TRUE</span>) <span class="co"># convertir a factor</span></span>
<span id="cb25-9"><a href="#cb25-9" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb25-10"><a href="#cb25-10" aria-hidden="true" tabindex="-1"></a>proc_elsoc<span class="sc">$</span>jv_delincuentes <span class="ot">&lt;-</span> car<span class="sc">::</span><span class="fu">recode</span>(proc_elsoc<span class="sc">$</span>jv_delincuentes,</span>
<span id="cb25-11"><a href="#cb25-11" aria-hidden="true" tabindex="-1"></a> <span class="at">recodes =</span> <span class="fu">c</span>(<span class="st">"1 = 'Nunca';</span></span>
<span id="cb25-12"><a href="#cb25-12" aria-hidden="true" tabindex="-1"></a><span class="st"> 2 = 'Pocas veces';</span></span>
<span id="cb25-13"><a href="#cb25-13" aria-hidden="true" tabindex="-1"></a><span class="st"> 3 = 'Algunas veces';</span></span>
<span id="cb25-14"><a href="#cb25-14" aria-hidden="true" tabindex="-1"></a><span class="st"> 4 = 'Muchas veces';</span></span>
<span id="cb25-15"><a href="#cb25-15" aria-hidden="true" tabindex="-1"></a><span class="st"> 5 = 'Siempre';</span></span>
<span id="cb25-16"><a href="#cb25-16" aria-hidden="true" tabindex="-1"></a><span class="st"> -888 = NA; </span></span>
<span id="cb25-17"><a href="#cb25-17" aria-hidden="true" tabindex="-1"></a><span class="st"> -999 = NA"</span>),</span>
<span id="cb25-18"><a href="#cb25-18" aria-hidden="true" tabindex="-1"></a> <span class="at">as.factor =</span> <span class="cn">TRUE</span>, <span class="co"># convertir a factor</span></span>
<span id="cb25-19"><a href="#cb25-19" aria-hidden="true" tabindex="-1"></a> <span class="at">levels =</span> <span class="fu">c</span>(<span class="st">"Nunca"</span>,</span>
<span id="cb25-20"><a href="#cb25-20" aria-hidden="true" tabindex="-1"></a> <span class="st">"Pocas veces"</span>,</span>
<span id="cb25-21"><a href="#cb25-21" aria-hidden="true" tabindex="-1"></a> <span class="st">"Algunas veces"</span>,</span>
<span id="cb25-22"><a href="#cb25-22" aria-hidden="true" tabindex="-1"></a> <span class="st">"Muchas veces"</span>,</span>
<span id="cb25-23"><a href="#cb25-23" aria-hidden="true" tabindex="-1"></a> <span class="st">"Siempre"</span>))<span class="co"># ordenamos niveles</span></span>
<span id="cb25-24"><a href="#cb25-24" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb25-25"><a href="#cb25-25" aria-hidden="true" tabindex="-1"></a>proc_elsoc</span></code><button title="Copiar al portapapeles" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 980 × 5
edad sexo ingreso autor_democ jv_delincuentes
Expand Down
Loading

0 comments on commit bbea644

Please sign in to comment.