-
Notifications
You must be signed in to change notification settings - Fork 7
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
sum(is.na()) helpers #2
Comments
Are you looking to do something really efficient in cpp or would something like the following suffice? df <- tibble::tibble(x = c(NA, NA, NA, 1.6, 1.8),
y = c(NA, 5, 9, 10, NA))
df
#> # A tibble: 5 × 2
#> x y
#> <dbl> <dbl>
#> 1 NA NA
#> 2 NA 5
#> 3 NA 9
#> 4 1.6 10
#> 5 1.8 NA
# n_absent ------------------------------------------------------------
sum(is.na(df$x))
#> [1] 3
sum(is.na(df$y))
#> [1] 2
n_absent <- function(x) sum(is.na(x))
n_absent(df$x)
#> [1] 3
n_absent(df$y)
#> [1] 2
# n_present -----------------------------------------------------------
sum(!(is.na(df$x)))
#> [1] 2
sum(!(is.na(df$y)))
#> [1] 3
n_present <- function(x) sum(!(is.na(x)))
n_present(df$x)
#> [1] 2
n_present(df$y)
#> [1] 3 |
It can be a little more efficient if done in C, but basically that. |
Just a friendly note that some of these helpers are in library(naniar)
n_miss(airquality)
#> [1] 44
n_miss(airquality$Ozone)
#> [1] 37
n_complete(airquality)
#> [1] 874
n_complete(airquality$Ozone)
#> [1] 116
prop_miss(airquality)
#> [1] 0.04793028
prop_miss(airquality$Ozone)
#> [1] 0.2418301
prop_complete(airquality)
#> [1] 0.9520697
prop_complete(airquality$Ozone)
#> [1] 0.7581699
pct_miss(airquality)
#> [1] 4.793028
pct_miss(airquality$Ozone)
#> [1] 24.18301
pct_complete(airquality)
#> [1] 95.20697
pct_complete(airquality$Ozone)
#> [1] 75.81699 |
Wanted to add a note here from r-lib/rlang#558 A verb/function that does always return a data.frame / matrix:
And quoting @hadley :
|
I think we'll just stick with |
@hadley Should we try to maintain a different prefix between |
I suspect that ship has already sailed, but it’s worth thinking about. |
n_absent()
andn_present()
The text was updated successfully, but these errors were encountered: