0

I am trying to make the table below show a "Y" if the cell is populated. And if the cell is empty it is recoded as an "N"

Below I have provided a image along with the code I am using. In the image, the left column is what I am working with, and the right column is what I'm wanting it to be recoded as.

enter image description here

df <- df %>% 
  mutate(column1 = recode(column1,
                               " " = "N",
                               "If populated" = "Y"))

Any help is appreciated.

1
  • df %>% mutate(column1 = ifelse(column1 == " ", "N", "Y")) ? Commented Mar 21, 2023 at 22:26

1 Answer 1

0

You can use the if_else() function from dplyr to create the new column, though the exact syntax depends on what the blank values are actually represented by in your data frame.

# If the values are stored as NA:
df <- df |>
  mutate(recode = if_else(is.na(column1), 'n', 'y'))

# If the values are stored as empty strings:
df <- df |>
  mutate(recode = if_else(column1 == '', 'n', 'y'))

# If the values are stored as spaces:
df <- df |>
  mutate(recode = if_else(column1 == ' ', 'n', 'y'))
Sign up to request clarification or add additional context in comments.

2 Comments

ifelse is not part of dplyr
Whoops! Yes, I was thinking of if_else. Updating now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.