Reputation: 462
Okey, after staring for hours at a dplyr cheat sheet I'm now finally giving up and asking my question here.
I have data in this form:
ID Material Supplier
1 a 01
2 b 02
3 NA 01
4 NA 02
4 a 02
4 b 02
Now, I want to replace the NAs with the most common material grouped by supplier. I'm using this code to count the most common materials and place the count in a data frame b but I want to do the replacement at the same time.
b <- a[which(!is.na(a$material)),] %>%
group_by(supplier, material) %>%
summarise(n = n()) %>%
group_by(supplier) %>%
filter(n == max(n))
Thanks in advance...
Upvotes: 2
Views: 89
Reputation: 3875
You could do it using table
within the dplyr
function:
a %>%
group_by(Supplier) %>%
mutate(Material=ifelse(is.na(Material),names(sort(table(Material),dec=T))[1],Material))
This returns:
ID Material Supplier
<int> <chr> <int>
1 1 a 1
2 2 b 2
3 3 a 1
4 4 b 2
5 4 a 2
6 4 b 2
Upvotes: 2