group_by() into fill() not working as expected

Luckily you can still use zoo::na.locf for this:

df %>% 
    group_by(id) %>% 
    mutate(email = zoo::na.locf(email, na.rm = FALSE))  
# Source: local data frame [6 x 2]
# Groups: id [3]
# 
#      id         email
#   (dbl)        (fctr)
# 1     1 [email protected]
# 2     1 [email protected]
# 3     2 [email protected]
# 4     2 [email protected]
# 5     3            NA
# 6     3            NA

Another option is to use do from dplyr:

df3 <- df %>% group_by(id) %>% do(fill(.,email))

Looks like this has been fixed in the development version of tidyr. You now get the expected result per id using fill from tidyr_0.3.1.9000.

df %>% group_by(id) %>% fill(email)

Source: local data frame [6 x 2]
Groups: id [3]

     id         email
  (dbl)        (fctr)
1     1 [email protected]
2     1 [email protected]
3     2 [email protected]
4     2 [email protected]
5     3            NA
6     3            NA

Tags:

R

Dplyr

Tidyr