Reputation: 23
I'm working with Pima Indians Diabetes data from Kaggle in Rstudio and instead of na's as missing values it has 0s.
How can I count the number of "0" values in each variable with a single loop instead of typing table(data$variableName==0)
for each column. Just rephrasing ,"a single loop for the whole data frame".
Upvotes: 2
Views: 1027
Reputation: 1250
Try this:
library(dplyr)
data %>% summarise(across(.fns = ~sum(.==0,na.rm=TRUE) ,.names = "Zeros_in_{.col}"))
Upvotes: 1
Reputation: 887881
We can use colSums
on a logical matrix
colSums(data == 0)
Or with sapply
in a loop
sapply(data, function(x) sum(x == 0))
or with apply
apply(data, 2, function(x) sum(x == 0))
Or in a for
loop
count <- numeric(ncol(data))
for(i in seq_along(data)) count[i] <- sum(data[[i]] == 0)
Upvotes: 3