Why won't this work?? summarize(across

Hi,

I just want to count the NAs in the below columns using the summarize across function, not sure why it doesn't work..

Dataset = data.frame(
  A = c(1,2,3,NA),
  B = c(1,2,NA,NA),
  C = c(1,2,3,4)  
)


Dataset %>%
  summarize(across(c(A,B,C), count(is.na(.))))

I had tried ~count(is.na()) but doesn't seem to work.

Error: Problem with `summarise()` input `..1`.
x no applicable method for 'count' applied to an object of class "logical"

Looks like the error message says R can't count the columns which became logical columns, so I tried this.

Dataset %>%
  summarize(across(c(A,B,C), ~count(is.na==TRUE)))

but still doesn't seem to work, any help would be great!

I think you want

Dataset %>%
  summarize(across(c(A,B,C), ~sum(is.na(.))))

dplyr::count() counts observations in a dataframe. You're looking for sum() because you're counting a vector.

Dataset %>%
  summarize(across(c(A,B,C), ~sum(is.na(.))))
1 Like

This topic was automatically closed 7 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.