Created
September 17, 2020 09:53
-
-
Save mark-andrews/53345e7040488ad0e5169a5cce34d80e to your computer and use it in GitHub Desktop.
A few examples of filtering out missing values in a R data frame
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
df <- tibble(d13C = c(1, NA, 3, 4, NA, 6), | |
d15N = c(1, 2, NA, 4, NA, 6), | |
d42 = c(1, NA, NA, 4, 5, NA)) | |
# to remove all rows from `df` that have missing values in `d13C` | |
# but keep rows that have missing values in any other variable: | |
filter(df, !is.na(d13C)) | |
# to remove all rows with NAs in `d13C` and `d15N`, | |
# but keep rows that have missing values in any other variable: | |
# In other words, remove a remove if the value of `d13C` and `d15N` are *both* NA, | |
# but keep the row if only one of `d13C` and `d15N` is NA, and also keep the row | |
# if the other variable (`d42`) is NA | |
filter_at(df, vars(d13C, d15N), any_vars(!is.na(.))) | |
# But if we want to remove any rows where *either* `d13C` and `d15N` are NA, but | |
# keep the row if the other variable (`d42`) is NA, then we would do this. | |
filter_at(df, vars(d13C, d15N), all_vars(!is.na(.))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment