0

I have a data frame whose columns I want to retain as is, but mutate nonsensical values (i.e. daily calorie intake = 88888) to NA. I am trying to do this with dplyr, but cannot find a solution which combines the column names as a list or that match a prefix (all start with "DRPN"), and change the 88888 values to NA.

Tried mutate_if() but the function will not take the list of columns.

Tried mutate_at() but the function doesn't accept my requirements.

df_recode <- lab1_final %>%
          select(vars(starts_with("DRPN")) %>%
          mutate_if(vars, "88888", NA)

Which I am trying to get to swap the 88888 observations with NA so that I can properly analyze the continuous variables. Any ideas?

Walker
  • 11
  • 1

1 Answers1

2

Here, we can use mutate_at instead of mutate_if. Also, if the intention is to mutate only selected columns, then we don't need the select before

library(dplyr)
lab1_final %>%
      mutate_at(vars(starts_with("DRPN")), na_if, y = "88888")

However, if we are interested in only select a subset of columns and return only those, use trasmute_at (this can avoid one step - or select with mutate_all)

lab1_final %>%
      transmute_at(vars(starts_with("DRPN")), na_if, y = "88888")

The quote is not needed 88888 if there are numeric columns.

akrun
  • 874,273
  • 37
  • 540
  • 662