[英]Count non-NA values by each column in dplyr using summarize_if
I want to count the number of non-missing rows in each column that meets a criteria.我想计算每列中满足条件的非缺失行数。 Below is the code I currently have.
以下是我目前拥有的代码。
data(mtcars)
mtcars %>%
summarize_if(is.numeric, sum(!is.na()))
Please be aware that summarise_if
has been superseded, so it's recommended to use across
and where
instead.请注意
summarise_if
已被取代,因此建议改用across
和where
。 However, your original code was close, you just needed a tilde ( ~
).但是,您的原始代码很接近,您只需要波浪号 (
~
)。
The dataset mtcars
has no missing values, so I used airquality
instead.数据集
mtcars
没有缺失值,所以我改用airquality
。
library(tidyverse)
data(airquality)
airquality %>%
summarise(across(where(is.numeric), ~ sum(!is.na(.x))))
#> Ozone Solar.R Wind Temp Month Day
#> 1 116 146 153 153 153 153
airquality %>%
summarise_if(is.numeric, ~ sum(!is.na(.x)))
#> Ozone Solar.R Wind Temp Month Day
#> 1 116 146 153 153 153 153
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.