Skip to content

Instantly share code, notes, and snippets.

@jimbrig
Last active June 7, 2020 18:43
Show Gist options
  • Save jimbrig/2efe2c55d264121106f94e9e15643e5c to your computer and use it in GitHub Desktop.
Save jimbrig/2efe2c55d264121106f94e9e15643e5c to your computer and use it in GitHub Desktop.
Fill in values by group where there are missings / NAs
library(dplyr)
df <- data.frame(id = c(1,1,1,2,2,2), value = c(1,NA,NA,NA,NA,777))
df %>%
group_by(id) %>%
mutate(
value2 = first(value[!is.na(value)])
)
# This will fill using the first non-missing value withing a given subject.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment