0

I am currently using station data for my research in R, and I need to count the number of missing/null values for each month. The data is currently in daily measurements, and the monthly total of missing values would let me trim certain months out if they are not useful.

CUM00078310_df %>% 
  dplyr::mutate(
    Month=month(Date), 
    Mis = rowSums(is.na(.[,grepl("C",colnames(CUM00078310_df))]))
  ) %>% 
  group_by(Month) %>% 
  summarize(Sum=sum(Mis), Percentage=mean(Mis))
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 4
    Please, provide a minimal reproducible example: [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – PaulS Jul 06 '22 at 16:33
  • Does this answer your question? [counting grouped missing values in R](https://stackoverflow.com/questions/45345491/counting-grouped-missing-values-in-r) – Limey Jul 06 '22 at 16:38
  • 3
    Welcome to SO, Katie Giesa! Questions on SO (especially in R) do much better if they are reproducible and self-contained. By that I mean including sample representative data (perhaps via `dput(head(x))` or building data programmatically (e.g., `data.frame(...)`), possibly stochastically) and perhaps actual output (with verbatim errors/warnings) versus intended output. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Jul 06 '22 at 16:46

1 Answers1

0

Here is an example. Not sure if you want the data summarized or held within the dataframe. If not summarized, then omit final two lines of code. Add month grouping variable to group_by() with your data. Filter NA's only, if needed filter(is.na(x))


df<-data.frame(x = c(NA,2,5,10,15, NA, 3, 5, 10, 15, NA, 4, 10, NA, 6, 15))

df <- df %>% 
  group_by(x) %>% 
  mutate(valueCount =  n()) %>% 
  arrange(desc(valueCount)) %>% 
  group_by(x, valueCount) %>% 
  summarise()

summarized example

df<-data.frame(x = c(NA,2,5,10,15, NA, 3, 5, 10, 15, NA, 4, 10, NA, 6, 15))

Unsummarized example
df <- df %>% 
  group_by(x) %>% 
  mutate(valueCount =  n()) %>% 
  arrange(desc(valueCount))

additional example

Susan Switzer
  • 1,531
  • 8
  • 34