Home > Back-end >  how to include columns that might not exist in crosstab/frequency table?
how to include columns that might not exist in crosstab/frequency table?

Time:11-21

I want to combine statement that can include columns that might not exist in a frequency table, something I've tried like putting: dat %>% intersect(names(.), c("A", "B","C")) or dat %>% select(any_of(c("A", "B", "C"))), together with tabyl or count. But none of them will work, since I don't know what column will be missing, and can't specify which columns to tabulate, which could end up in either a 1,2 or 3 way crosstab (For example, it could be either "a" missing, or "b", or both "a" and "b", or all 3 columns missing). I also tried using map(tabyl) but that will end up giving the frequency of every single column. I think if else statement will be lengthy if there are too many crosstabs that I want to look at. Any suggestions?

dput(dat)
structure(list(A = c("2", "7", "1", "1", "1"), C = c(NA, NA, NA, NA, NA)), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"))

desired output if B is missing:

in count format:

             A      C     n
           <dbl>  <dbl> <int>
 1             2     NA   1
 2             7     NA   1
 3             1     NA   3

or in tabyl format:

A   1   2   7
NA  3   1   1

CodePudding user response:

You can do (the example only checks against letters A, B, C, so this might be different in your real-life use case):

library(tidyverse)

existing_cols <- intersect(LETTERS[1:3], names(dat))

dat |> 
  count(!!!syms(existing_cols))

# A tibble: 3 × 3
  A     C         n
  <chr> <lgl> <int>
1 1     NA        3
2 2     NA        1
3 7     NA        1
  •  Tags:  
  • r
  • Related