Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace columns that have NA values with desired value #5

Open
ghost opened this issue Dec 5, 2018 · 2 comments
Open

Replace columns that have NA values with desired value #5

ghost opened this issue Dec 5, 2018 · 2 comments

Comments

@ghost
Copy link

ghost commented Dec 5, 2018

You want to specify certain columns that contain NA and replace it with another value such as 0.

For example, smiths, a dataset contained in tidyr, has a couple of NA values in age and weight columns.

smiths
# A tibble: 2 x 5
  subject     time   age weight height
  <chr>      <dbl> <dbl>  <dbl>  <dbl>
1 John Smith     1    33     90   1.87
2 Mary Smith     1    NA     NA   1.54

Solution {-}

smiths %>%
  replace_na(list(age = 0, weight = 0))
# A tibble: 2 x 5
  subject     time   age weight height
  <chr>      <dbl> <dbl>  <dbl>  <dbl>
1 John Smith     1    33     90   1.87
2 Mary Smith     1     0      0   1.54

Discussion {-}

If data is a dataframe or tibble, you need to create a list inside replace_na with the name of columns on the left hand side and value to replace NA with on the right hand side.

If data is a vector, you insert a single value used for replacement.

@garrettgman
Copy link
Contributor

@jasonbaik94 since you are providing full recipes, can you just go ahead and submit them as pull requests?

I'd prefer to leave issues for people who want to request a recipe, but cannot provide a solution or discussion. Thanks!

@ghost
Copy link
Author

ghost commented Dec 5, 2018

Ohhh gotcha. I'll submit them as pull requests from now on ☺️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant