Author

Zoë Turner

Published

March 12, 2024

Modified

March 12, 2024

Create data

This was written originally in an Excel spreadsheet and used {datapasta} to copy into R as code to build the same data frame. {datapasta} can be access through RStudio as an Addin as well as code. Find out more about {datapasta} from the Introduction to R and R Studio course.

Recoding to NA

survey <- tibble::tribble(
  ~Survey.Response, ~Code,
       "Response1",   -9L,
       "Response2",    2L,
       "Response3",   10L,
       "Response4",    0L,
       "Response5",    5L,
       "Response6",   -9L,
        "Missing", NA
  )

Recode to NA

library(tidyverse)

survey |> 
  mutate(new_column = na_if(Code, -9))
# A tibble: 7 × 3
  Survey.Response  Code new_column
  <chr>           <int>      <int>
1 Response1          -9         NA
2 Response2           2          2
3 Response3          10         10
4 Response4           0          0
5 Response5           5          5
6 Response6          -9         NA
7 Missing            NA         NA

It’s also possible to use the numbers and case_when():

survey |> 
1  mutate(new_column = case_when(Code < 0 ~ NA,
2                                Code == 0 ~ 1000,
3                                .default = Code))
1
Where Code is less than 0 then code to NA.
2
Where Code is equal to 0 then recode to 1000 which is a number that will stand out.
3
For everything else return the original data from column Code.
# A tibble: 7 × 3
  Survey.Response  Code new_column
  <chr>           <int>      <dbl>
1 Response1          -9         NA
2 Response2           2          2
3 Response3          10         10
4 Response4           0       1000
5 Response5           5          5
6 Response6          -9         NA
7 Missing            NA         NA

Or ifelse() where there are only two options:

survey |> 
  mutate(new_column = ifelse(Code < 0, NA, Code))
# A tibble: 7 × 3
  Survey.Response  Code new_column
  <chr>           <int>      <int>
1 Response1          -9         NA
2 Response2           2          2
3 Response3          10         10
4 Response4           0          0
5 Response5           5          5
6 Response6          -9         NA
7 Missing            NA         NA

Recode from NA

survey |> 
  mutate(new_column2 = replace_na(Code, 1000))
# A tibble: 7 × 3
  Survey.Response  Code new_column2
  <chr>           <int>       <int>
1 Response1          -9          -9
2 Response2           2           2
3 Response3          10          10
4 Response4           0           0
5 Response5           5           5
6 Response6          -9          -9
7 Missing            NA        1000
Back to top

Reuse

CC0

Citation

For attribution, please cite this work as:
Turner, Zoë. 2024. “Recoding an NA and Back Again.” March 12, 2024. https://nhsrcommunity.com/blog/coding-to-and-from-NA.html.