A convenient way to replace NAs by group.

na_replace_grouped(df, group_by_cols = NULL, ...)

Arguments

df

A data.frame object for which grouped NA replacement is desired.

group_by_cols

The column(s) used to use for the grouping.

...

Other arguments to `na_replace`

Value

A `data.frame` object with `NA`s replaced.

Examples

test2 <- data.frame(A=c("A","A","A","B","B","B"),
B=c(NA,5,2,2,NA,2))
head(na_replace_grouped(test2,"A",how="value","Replaced"))
#>   A        B
#> 1 A Replaced
#> 2 A        5
#> 3 A        2
#> 4 B        2
#> 5 B Replaced
#> 6 B        2