Skip to content

Commit

Permalink
Change fct_recode() to recode()
Browse files Browse the repository at this point in the history
  • Loading branch information
wch committed Aug 1, 2018
1 parent 0b339f4 commit be80b9b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
14 changes: 5 additions & 9 deletions ch06.Rmd
Expand Up @@ -117,15 +117,12 @@ For this example, we used the `birthwt` data set. It contains data about birth w
birthwt
```

One problem with the faceted graph is that the facet labels are just 0 and 1, and there's no label indicating that those values are for whether or not smoking is a risk factor that is present. To change the labels, we need to change the names of the factor levels. First we'll take a look at the factor levels, then we'll assign new factor level names in the same order, and save this new data set as `birthwt_mod`:
One problem with the faceted graph is that the facet labels are just 0 and 1, and there's no label indicating that those values are for whether or not smoking is a risk factor that is present. To change the labels, we change the names of the factor levels. First we'll take a look at the factor levels, then we'll assign new factor level names in the same order, and save this new data set as `birthwt_mod`:

```{r}
library(forcats) # Load the forcats package to help you work with factor variables
birthwt_mod <- birthwt %>%
mutate(smoke = as.factor(smoke)) %>% # Convert smoke to a factor
mutate(smoke = fct_recode(smoke, 'No Smoke' = "0", 'Smoke' = "1"))
birthwt_mod <- birthwt
# Convert smoke to a factor and reassign new names
birthwt_mod$smoke <- recode_factor(birthwt_mod$smoke, '0' = 'No Smoke', '1' = 'Smoke')
```

Now when we plot our modified data frame, our desired labels appear (Figure \@ref(fig:FIG-DISTRIBUTION-MULTI-HISTOGRAM-FACET-LABELS)).
Expand Down Expand Up @@ -305,8 +302,7 @@ One problem with the faceted graph is that the facet labels are just 0 and 1, an
levels(birthwt_mod$smoke)
#> [1] "0" "1"
birthwt_mod <- birthwt_mod %>%
mutate(smoke = recode(smoke, '0' = 'No Smoke', '1' = 'Smoke'))
birthwt_mod$smoke <- recode(birthwt_mod$smoke, '0' = 'No Smoke', '1' = 'Smoke')
```

Now when we plot our modified data frame, our desired labels appear (Figure
Expand Down
2 changes: 1 addition & 1 deletion ch15.Rmd
Expand Up @@ -777,7 +777,7 @@ library(forcats)
fct_recode(pg$group, No = "ctrl", Yes = "trt1", Yes = "trt2")
```

Another difference is that `fct_recode()` will always return a factor, whereas `recode()` will return a character vector if it is given a character vector, and will return a factor if it is given a factor.
Another difference is that `fct_recode()` will always return a factor, whereas `recode()` will return a character vector if it is given a character vector, and will return a factor if it is given a factor. (Although dplyr does have a `recode_factor()` function which also always returns a factor.)


Using base R, recoding can be done with the `match()` function:
Expand Down

0 comments on commit be80b9b

Please sign in to comment.