Last active
June 7, 2020 18:43
-
-
Save jimbrig/2efe2c55d264121106f94e9e15643e5c to your computer and use it in GitHub Desktop.
Fill in values by group where there are missings / NAs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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