getting count of missing values in summary

i want to get the count of NA's in column with max and min. is there any way in expss already to get a count in below example

library(expss)
library(dplyr)

example <- data.frame(org = c("A", "B", "C", "D", "E", "F", "G"),
                      q1 = c("apple", "apple", NA, "apple", "apple", "apple", NA),
                      q2 = c(NA, NA, NA, "banana", "banana", "banana", NA),
                      q3 = c("orange", NA, "orange", "orange", NA, "orange", NA),
                      region = c("1", "1", "2", "3", "3", "2", "2"))





pct_all <- cross_cpct(example, mrset(q1 %to% q3), region)](https://)

output should be look like

image

To count up the missing values in each of your columns, I would use the below code (In this case, for q1):

sum(is.na(example$q1))

The is.na part of this function will return 1 for missing values and 0 for non-missing values. The sum function will then return the sum of these binary values, giving you the total number of missing values in the column of your choosing.

thats is for one variable but i want to do it for mrset(q1 %to% q3)

I'm guessing it might be

library(expss)
library(dplyr)

example <- data.frame(org = c("A", "B", "C", "D", "E", "F", "G"),
                      q1 = c("apple", "apple", NA, "apple", "apple", "apple", NA),
                      q2 = c(NA, NA, NA, "banana", "banana", "banana", NA),
                      q3 = c("orange", NA, "orange", "orange", NA, "orange", NA),
                      region = c("1", "1", "2", "3", "3", "2", "2"))

na_replace_string <- "z_na"

(solid_example <- mutate(example,
                        across(starts_with("q"),
                               \(x)ifelse(is.na(x),na_replace_string,x))))

(pct_all <- cross_cpct(solid_example,
                       mrset(q1 %to% q3), region))

pct_all$row_labels[which(pct_all$row_labels==na_replace_string)] <- "#missing vals"

pct_all
 |               | region |       |     |
 |               |      1 |     2 |   3 |
 | ------------- | ------ | ----- | --- |
 |         apple |    100 |  33.3 | 100 |
 |        banana |        |  33.3 | 100 |
 |        orange |     50 |  66.7 |  50 |
 | #missing vals |    150 | 166.7 |  50 |
 |  #Total cases |      2 |   3.0 |   2 |

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.