Nick Knauer
Nick Knauer

Reputation: 4243

Replace NA values with average by group with filter

I have a dataset below:

head(weather_data)

  dmanum DATE       Avg_precipitation Avg_TAVG 
  <chr>  <date>                 <dbl>    <dbl>               
1 501    2017-01-01          0.000976     45.3               
2 501    2017-01-02                NA     39.3                
3 501    2017-01-03             0.366       42                
4 502    2017-01-01                NA       46                
5 502    2017-01-02                NA     33.3                
6 502    2017-01-03                NA     31.3                
7 503    2017-01-01                 5       46                
8 503    2017-01-02                10     33.3                
9 503    2017-01-03                15     31.3                

There are many values for dmanum with the same date. Based on my selection of dmanum, I want to take the average Avg_precipitation by week and replace the NA's for that specific DMA.

For example, if I were to use this dataset, I would try something like this but I am getting an error:

weather_data1<- weather_data %>%
  group_by(DATE) %>% 
  filter(., dmanum==502) %>%
  mutate_at(Avg_precipitation = na.fill(mean(Avg_precipitatation))

The expected output is this:

  dmanum DATE       Avg_precipitation Avg_TAVG 
  <chr>  <date>                 <dbl>    <dbl>               
1 501    2017-01-01          0.000976     45.3                
2 501    2017-01-02                NA     39.3                
3 501    2017-01-03             0.366       42                
4 502    2017-01-01            2.5004       46                
5 502    2017-01-02                10     33.3                
6 502    2017-01-03             7.683     31.3                
7 503    2017-01-01                 5       46                
8 503    2017-01-02                10     33.3                
9 503    2017-01-03                15     31.3                

Upvotes: 2

Views: 480

Answers (1)

akrun
akrun

Reputation: 886938

We can use replace after the group_by. Instead of filtering the rows, specify the logic in the list argument of replace to replace only those NAs where the 'dmanum' is 502

library(tidyverse)
weather_data %>%
       group_by(DATE) %>%
       mutate(Avg_precipitation = replace(Avg_precipitation,  
           is.na(Avg_precipitation) & dmanum == 502, 
          mean(Avg_precipitation, na.rm = TRUE)))
# A tibble: 9 x 4
# Groups:   DATE [3]
#  dmanum DATE       Avg_precipitation Avg_TAVG
#   <int> <date>                 <dbl>    <dbl>
#1    501 2017-01-01          0.000976     45.3
#2    501 2017-01-02         NA            39.3
#3    501 2017-01-03          0.366        42  
#4    502 2017-01-01          2.50         46  
#5    502 2017-01-02         10            33.3
#6    502 2017-01-03          7.68         31.3
#7    503 2017-01-01          5            46  
#8    503 2017-01-02         10            33.3
#9    503 2017-01-03         15            31.3

Upvotes: 2

Related Questions