Recoding missing data in longitudinal data frames with R -


i have data frame similar longitudinal structure data:

data = data.frame (    id = c("a","a","a","b","b","b","c","c", "c"),     period = c(1,2,3,1,2,3,1,2,3),    size = c(3,3,na, na, na,1, 14,14, 14)) 

the values of variable size fixed each period has same value size. yet observations have missing values. aim consists of replacing these missing values value of size associated periods there no missing (e.g. 3 id "a" , 1 id "b").

the desired data frame should similar to:

data.1       id period value            1     3            2     3            3     3       b      1     1       b      2     1       b      3     1       c      1    14       c      2    14       c      3    14 

i have tried different combinations of formula below don't result looking for.

library(dplyr)  data.1 = data %>% group_by(id) %>%    mutate(new.size = ifelse(is.na(size), !is.na(size),                           ifelse(!is.na(size), size, 0))) 

that yields following:

data.1 source: local data frame [9 x 4] groups: id [3]        id period  size new.size   (fctr)  (dbl) (dbl)    (dbl) 1           1     3        3 2           2     3        3 3           3    na        0 4      b      1    na        0 5      b      2    na        0 6      b      3     1        1 7      c      1    14       14 8      c      2    14       14 9      c      3    14       14 

i grateful if give me hint on how right solution.

here solution using dplyr na.omit

group_by(data, id) %>%      mutate(value=na.omit(size)[1]) source: local data frame [9 x 4] groups: id [3]        id period  size value   <fctr>  <dbl> <dbl> <dbl> 1           1     3     3 2           2     3     3 3           3    na     3 4      b      1    na     1 5      b      2    na     1 6      b      3     1     1 7      c      1    14    14 8      c      2    14    14 9      c      3    14    14 

note can replace na.omit max(size, na.rm=true) if looking maximum example.


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -