tell us

Dplyr::recode is from the dplyr package. The dplyr package provides a basic set of tools that makes it easier to work with data. The dplyr package itself is part of the tidyverse package.

The dplyr recode() function allows us to replace numeric, character and factor values with the desired values/names. However, this only applies to individual values in a specific column.

This is very helpful for psychology students when wanting to: - reverse score certain items e.g. changing values 1, 2, 3 into 3, 2, 1 on a scale - replace numbers with names e.g. changing 1 into females and 2 into males for a column coding for sex

show us

install and load packages

library(dplyr)
library(palmerpenguins)
library(tidyverse)

get some data

penguins <- penguins %>% na.omit()
print(penguins)
## # A tibble: 333 x 8
##    species island    bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
##    <fct>   <fct>              <dbl>         <dbl>             <int>       <int>
##  1 Adelie  Torgersen           39.1          18.7               181        3750
##  2 Adelie  Torgersen           39.5          17.4               186        3800
##  3 Adelie  Torgersen           40.3          18                 195        3250
##  4 Adelie  Torgersen           36.7          19.3               193        3450
##  5 Adelie  Torgersen           39.3          20.6               190        3650
##  6 Adelie  Torgersen           38.9          17.8               181        3625
##  7 Adelie  Torgersen           39.2          19.6               195        4675
##  8 Adelie  Torgersen           41.1          17.6               182        3200
##  9 Adelie  Torgersen           38.6          21.2               191        3800
## 10 Adelie  Torgersen           34.6          21.1               198        4400
## # ... with 323 more rows, and 2 more variables: sex <fct>, year <int>

use the function

The following example would not be practical for the penguins dataset since the dataframe is already presented very clearly.

To use the recode() function, the specific dataset/column is specified first followed by the original value/name and then the desired value/name to substitute in. The desired value/name to be substituted in must be in quotation marks. For example, penguins$island is the specific column, Biscoe is the original name coded in the column and “B” is the desired name to be substituted in.

rec_island <- dplyr::recode(penguins$island, Biscoe = "B", Dream = "D", Torgersen = "T")
print(rec_island)
##   [1] T T T T T T T T T T T T T T T B B B B B B B B B B D D D D D D D D D D D D
##  [38] D D D D D D D B B B B B B B B B B B B B B B B B B T T T T T T T T T T T T
##  [75] T T T T D D D D D D D D D D D D D D D D B B B B B B B B B B B B B B B B T
## [112] T T T T T T T T T T T T T T T D D D D D D D D D D D D D D D D D D D D B B
## [149] B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B
## [186] B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B
## [223] B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B B
## [260] B B B B B B D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D
## [297] D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D
## Levels: B D T

more resources

We used the Rdocumentation website (https://www.rdocumentation.org/packages/dplyr/versions/0.7.8/topics/recode) and the help panel in R (via ?recode in console).